声明一个长方体类Box,该类有长(Length)、宽(width)、高(high)三个数据成员,类中有获取和显示长、宽、高的成员函数,还有计算长方体表面积和体积的成员函数。请按上述要求声明该长方体类,并在主函数中定义该类的三个对象,并输出显示这3个长方体的长、宽、高信息,计算其面积和体积
时间: 2024-02-16 13:00:48 浏览: 36
好的,以下是 Box 类的声明:
```cpp
class Box {
private:
double length;
double width;
double high;
public:
Box(double len = 0, double wid = 0, double hig = 0); // 构造函数
void setLength(double len); // 设置长
void setWidth(double wid); // 设置宽
void setHigh(double hig); // 设置高
double getLength(); // 获取长
double getWidth(); // 获取宽
double getHigh(); // 获取高
double getArea(); // 获取表面积
double getVolume(); // 获取体积
};
```
下面是 Box 类成员函数的实现:
```cpp
Box::Box(double len, double wid, double hig)
{
length = len;
width = wid;
high = hig;
}
void Box::setLength(double len)
{
length = len;
}
void Box::setWidth(double wid)
{
width = wid;
}
void Box::setHigh(double hig)
{
high = hig;
}
double Box::getLength()
{
return length;
}
double Box::getWidth()
{
return width;
}
double Box::getHigh()
{
return high;
}
double Box::getArea()
{
return 2 * (length * width + width * high + high * length);
}
double Box::getVolume()
{
return length * width * high;
}
```
最后,以下是主函数的实现:
```cpp
#include <iostream>
using namespace std;
int main()
{
Box box1(1.2, 3.4, 5.6);
Box box2(2.3, 4.5, 6.7);
Box box3(3.4, 5.6, 7.8);
cout << "box1: length=" << box1.getLength() << ", width=" << box1.getWidth() << ", high=" << box1.getHigh() << endl;
cout << "box2: length=" << box2.getLength() << ", width=" << box2.getWidth() << ", high=" << box2.getHigh() << endl;
cout << "box3: length=" << box3.getLength() << ", width=" << box3.getWidth() << ", high=" << box3.getHigh() << endl;
cout << "box1 area=" << box1.getArea() << ", volume=" << box1.getVolume() << endl;
cout << "box2 area=" << box2.getArea() << ", volume=" << box2.getVolume() << endl;
cout << "box3 area=" << box3.getArea() << ", volume=" << box3.getVolume() << endl;
return 0;
}
```
输出结果为:
```
box1: length=1.2, width=3.4, high=5.6
box2: length=2.3, width=4.5, high=6.7
box3: length=3.4, width=5.6, high=7.8
box1 area=94.24, volume=22.848
box2 area=157.64, volume=55.935
box3 area=268.96, volume=152.208
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)