#include<iostream>
using namespace std;
class Box
{
public:
Box(int h =8, int w = 8, int len = 9) : heigth(h), width(w), length(len){};//声明有默认参数的构造函数,用参数初始化表对数据成员初始化
int volume();
int area();
void input();
private:
int heigth;
int width;
int length;
};
int Box :: volume()
{
return (heigth * width * length);
}
int Box :: area()
{
return ((heigth * width + heigth * length + width * length) * 2);
}
void Box :: input()
{
cout << "请输入长方柱的长,宽,高:";
cin >> length >> width >> heigth;
}
int main()
{
Box a[5] = {
Box(7, 9, 8),
Box(12, 13, 21),
Box(11, 12, 16),
Box(),
Box()
};
for(int i = 0; i < 4; ++i)
{
cout << "第" << i + 1 << "长方柱类的体积是 " << a[i].volume() << endl;
cout << "第" << i + 1 << "长方柱类的表面积是 " << a[i].area() << endl << endl;
}
a[4].input();
cout << "第" << i + 1 << "长方柱类的体积是 " << a[i].volume() << endl;
cout << "第" << i + 1 << "长方柱类的表面积是 " << a[i].area() << endl << endl;
return 0;
}
第五周作业3
最新推荐文章于 2024-10-04 21:49:15 发布