作业帮 > 综合 > 作业

用面对对象的方法,编程实现求长方体的体积和表面积,数据成员包括length(长)、width(宽)、height(高),

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/09 02:51:02
用面对对象的方法,编程实现求长方体的体积和表面积,数据成员包括length(长)、width(宽)、height(高),要求用成员函数实现以下功能 1有一个无参构造函数和有参构造函数;2、由键盘输入长方体的长,宽,高;3,输入长方体的长宽高;4,计算长方体的表面积和体积并输出
/*输入长方体的长、宽、高(空格隔开) : 3 4 5Ca属性 :长 3宽 4高 5体积 : 60表面积 : 94Cb属性 :长 0宽 0高 0体积 : 0表面积 : 0Press any key to continue*/#include <iostream>
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;
}