代码:
#include <iostream>
using namespace std;
class Bulk
{
public:
void getdata();
double volume();
double areas();
private:
double length;
double width;
double height;
};
void Bulk::getdata()
{
cout<<"请分别输入长方体的长、宽、高:";
cin>>length>>width>>height;
}
double Bulk::volume()
{
double V;
V=length*width*height;
return V;
}
double Bulk::areas()
{
double S;
S=2*length*width+2*length*height+2*width*height;
return S;
}
int main()
{
Bulk B1,B2,B3;
B1.getdata();
B2.getdata();
B3.getdata();
cout<<"3个长方体的体积和表面积分别为:"<<endl;
cout<<B1.volume()<<' '<<B1.areas()<<endl;
cout<<B2.volume()<<' '<<B2.areas()<<endl;
cout<<B3.volume()<<' '<<B3.areas()<<endl;
return 0;
}
运行结果: