.编写一个长方体类(Cuboid),其数据成员有 length(长),width(宽),height(高),两个成员函数分别实现: (1)由键盘分别输入长、宽、高; (2)计算长方体的体积; (3)仿效教材中例9.1,为Cuboid添加默认构造函数。 并在主函数中定义该类的一个对象,并测试这些成员函数。
时间: 2024-03-17 07:39:49 浏览: 27
好的,根据您的要求,以下是长方体类的实现代码:
```cpp
#include <iostream>
using namespace std;
class Cuboid {
private:
double length;
double width;
double height;
public:
Cuboid() { // 默认构造函数
length = 0;
width = 0;
height = 0;
}
void input() { // 由键盘输入长、宽、高
cout << "请输入长方体的长、宽、高:" << endl;
cin >> length >> width >> height;
}
double volume() { // 计算长方体的体积
return length * width * height;
}
};
int main() {
Cuboid c; // 定义一个长方体对象
c.input(); // 输入长、宽、高
cout << "长方体的体积是:" << c.volume() << endl; // 计算并输出体积
return 0;
}
```
您可以编译运行上述代码,测试它是否符合您的要求。
相关问题
编写一个长方体类(Cuboid),其数据成员有 (长),width(宽),height(高),两个成员函数分别实现: (1)由键盘分别输入长、宽、高: (2)计算长方体的体积: length (3)为Cuboid添加默认构造函数。 并在主西数中定义该类的一个对象,并测试这些成员函数。
好的,以下是实现长方体类(Cuboid)的代码:
```cpp
#include <iostream>
using namespace std;
class Cuboid {
private:
double length;
double width;
double height;
public:
Cuboid() { // 默认构造函数
length = 0;
width = 0;
height = 0;
}
void input() { // 输入长、宽、高
cout << "请输入长方体的长、宽、高:";
cin >> length >> width >> height;
}
double volume() { // 计算长方体的体积
return length * width * height;
}
};
int main() {
Cuboid c1; // 定义一个长方体对象
c1.input(); // 输入长、宽、高
cout << "长方体的体积为:" << c1.volume() << endl; // 输出长方体的体积
return 0;
}
```
运行程序,你可以输入长、宽、高的值,程序会自动计算长方体的体积并输出。
定义一个名为Cuboid的长方体类,使其继承Rectangle类,其中包含一个表示高的double型成员变量height;定义一个构造方法Cuboid(double length,double width,double height);再定义一个求长方体体积的volume()方法。编写程序,求一个长、宽和高分别为10、5、2的长方体的体积。
好的,这是一个关于Java编程的问题。以下是代码实现:
```java
class Rectangle {
protected double length;
protected double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
}
public class Cuboid extends Rectangle {
private double height;
public Cuboid(double length, double width, double height) {
super(length, width);
this.height = height;
}
public double volume() {
return length * width * height;
}
public static void main(String[] args) {
Cuboid c = new Cuboid(10, 5, 2);
System.out.println("The volume of the Cuboid is: " + c.volume());
}
}
```
输出结果为:
```
The volume of the Cuboid is: 100.0
```
这里定义了一个名为`Cuboid`的类,继承了`Rectangle`类并添加了一个表示高的`height`成员变量。在构造方法中,使用`super(length, width)`调用`Rectangle`类的构造方法来初始化长和宽,再初始化高。定义了一个`volume()`方法来计算长方体的体积。在`main()`方法中创建一个长为10,宽为5,高为2的长方体对象,并输出其体积。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)