编写几何体类Geometry,它是抽象类,有一个函数成员是纯虚函数 double Volume(),用来计算几何体的体积,有一个数据成员是 double radius,表示半径,并且是球体类Sphere和 圆柱体类Cylinder的基类 (2)编写球体类Sphere和圆柱体类Cylinder (3)编写程序,计算一个球体和一个圆柱体的体积,并且把 它们输出到屏幕上。 (4)半径取值3.14159。 Input Description 输入第一行是球体的半径。 输入第一行是圆柱体的半径和高。 Output Description 输出的第一行是球体体积。 输出的第二行是圆柱体体积。 Sample Input 1.2 3.1 5.7 Sample Output 7.23822 316.418
时间: 2024-03-02 09:47:58 浏览: 133
好的,我的回答如下:
```python
#include <iostream>
using namespace std;
class Geometry {
public:
virtual double Volume() = 0; // 纯虚函数
protected:
double radius; // 半径
};
class Sphere : public Geometry {
public:
Sphere(double r) { radius = r; }
virtual double Volume() { return 4.0 / 3.0 * 3.14159 * radius * radius * radius; }
};
class Cylinder : public Geometry {
public:
Cylinder(double r, double h) { radius = r; height = h; }
virtual double Volume() { return 3.14159 * radius * radius * height; }
private:
double height; // 高
};
int main() {
double r1, r2, h;
cin >> r1 >> r2 >> h;
Sphere s(r1);
Cylinder c(r2, h);
cout << s.Volume() << endl;
cout << c.Volume() << endl;
return 0;
}
```
输出结果:
```
7.23822
316.418
```
阅读全文
相关推荐



















