定义立方体类box,数据成员有长宽高且都是整数,构造函数初始化数据成员,成员函数计算体积,主函数中输入长宽高,输出立方体体积。
时间: 2023-04-25 14:02:03 浏览: 134
好的,以下是用中文回复的定义立方体类box,数据成员有长宽高且都是整数,构造函数初始化数据成员,成员函数计算体积,主函数中输入长宽高,输出立方体体积:
```
class box {
private:
int length;
int width;
int height;
public:
box(int l, int w, int h) {
length = l;
width = w;
height = h;
}
int volume() {
return length * width * height;
}
};
int main() {
int l, w, h;
cin >> l >> w >> h;
box myBox(l, w, h);
cout << myBox.volume() << endl;
return ;
}
```
相关问题
定义立方体类Box,数据成员有长宽高且都是整数,构造函数初始化数据成员,成员函数计算体积,主函数中输入长宽高,输出立方体体
积。
实现如下:
```python
class Box:
def __init__(self, length, width, height):
self.length = length
self.width = width
self.height = height
def volume(self):
return self.length * self.width * self.height
length = int(input("请输入立方体的长:"))
width = int(input("请输入立方体的宽:"))
height = int(input("请输入立方体的高:"))
box = Box(length, width, height)
print("立方体的体积为:", box.volume())
```
用C++定义立方体类Box,数据成员有长宽高且都是整数,构造函数初始化数据成员,成员函数计算体积,主函数中输入长宽高,输出立方体体
积。
```c
#include <stdio.h>
typedef struct {
int length;
int width;
int height;
} Box;
void initBox(Box *box, int length, int width, int height) {
box->length = length;
box->width = width;
box->height = height;
}
int calculateVolume(Box *box) {
return box->length * box->width * box->height;
}
int main() {
int length, width, height;
printf("请输入长、宽、高:");
scanf("%d%d%d", &length, &width, &height);
Box box;
initBox(&box, length, width, height);
printf("立方体体积为:%d\n", calculateVolume(&box));
return 0;
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![.pdf](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![.pdf](https://img-home.csdnimg.cn/images/20210720083646.png)
![.pdf](https://img-home.csdnimg.cn/images/20210720083646.png)
![.pdf](https://img-home.csdnimg.cn/images/20210720083646.png)