求立方体体积和面积(类和对象的定义)
#include<iostream>
using namespace std;
class Box{
private:
float X;
public:
void seta(float x);
float getvolume();
float getarea();
void disp();
};
void Box::seta(float x)
{
X=x;
}
float Box::getarea()
{
return 6*X*X;
}
float Box::getvolume()
{
return X*X*X;
}
void Box::disp()
{
cout<<"立方体表面积为:"<<6*X*X<<endl<<"立方体体积为:"<<X*X*X<<endl;
}
int main()
{
float ab;
cout<<"请输入立方体边长:";
cin>>ab;
Box obj;
obj.seta(ab);
obj.getarea();
obj.getvolume();
obj.disp();
return 0;
}