- 定义一个Box(盒子)类,成员变量: length(长)、width(宽)和height(高) 默认值均为0;成员方法: 构造方法Box()有三个参数,设置盒子长、宽和高三个合理的初始数据(正数);方法getVolume()没有参数,计算并返回盒子的体积,方法getArea()没有参数,计算并返回盒子的表面积,方法setEdge()有三个参数,用于修改三条边长。定义TestBox类,在其main()方法中创建Box对象b,长宽高分别为3、4、5,求b的体积与表面积并输出;将b的边长修改为1、2、3,求b的体积与表面积并输出。******
#include <iostream>
using namespace std;
class Box {
float length, width, height;
public:
Box();
Box(float l, float w, float h);
float GetVolume() const;
};
Box::Box() : length(1), width(1), height(1) {}
Box::Box(float l, float w, float h)
: length(l), width(w), height(h) {}
float Box::GetVolume() const {
return height * width * length;
}
int main()
{
Box b1, b2(2, 3, 4);
float v1, v2;
v1 = b1.GetVolume();
v2 = b2.GetVolume();
if (v1>v2)
cout << v1 << " " << v2 << endl;
else
cout << v2 << " " << v1 << endl;
return 0;
}