面向对象 1)问题:设计一个长方体类Box,它能计算并输出长方体的体积

和表面积。具体要求如下:
(1)私有数据成员
float a,b,c; // 分别存放长方体的三条边长
float volume,area; //分别存放长方体的体积和表面积
(2)公有成员函数
Box(float p,float q,float r); //构造函数,为边长赋值
void getvolume(); //计算体积
void getarea(); //计算表面积
void disp() //输出结果
在主函数中定义对象obj,取边长分别为4,5,6,计算长方体的体积和表面积,并输出结果。

#include
using namespace std;

class Cuboid {
private :
int length;
int width;
int height;
public :
Cuboid() {length = width = height = 0;}
Cuboid(int l,int w,int h) {
length = l;
width = w;
height = h;
}
int Volume() { return length * width * height; }
int SuperficialArea() {
return 2 * (length * width + width * height + height * length);
}
void Show() {
cout << "长 " << length << endl;
cout << "宽 " << width << endl;
cout << "高 " << height << endl;
}
};

int main() {
int len,w,h;
cout << "输入长方体的长、宽、高(空格隔开) : ";
cin >> len >> w >> h;
Cuboid Ca(len,w,h),Cb;
cout << "Ca属性 :\n";
Ca.Show();
cout << "体积 : " << Ca.Volume() << endl;
cout << "表面积 : " << Ca.SuperficialArea() << endl;
cout << "Cb属性 :\n";
Cb.Show();
cout << "体积 : " << Cb.Volume() << endl;
cout << "表面积 : " << Cb.SuperficialArea() << endl;
return 0;
}
温馨提示:答案为网友推荐,仅供参考
相似回答