定义平面二维点类CPoint,有数据成员x坐标,y坐标,函数成员(构造函数复制构造函数、虚函数求面积GetArea,虚函数求体积函数GetVolume、输出点信息函数print。由CPoint类派生出圆类Cirle类(新增数据成员半径radius),函数成员(构造函数、复制构造函数、求面积GetArea,虚函数求体积函数GetVolume、输出圆信息函数print。 再由Ccirle类派生出圆柱体Ccylinder类(新增数据成员高度height),函数成员(构造函数、复制构造函数、求表面积GetArea,求体积函数GetVolume、输出圆柱体信息函数print。在主函数测试这个这三个类。 输入格式: 0 0 例如:第一行读入圆心坐标。 1 2 第二行读入半径与高度。 输出格式: 分四行输出,分别代表圆心、底面积、表面积、体积。 输入样例: 在这里给出一组输入。例如: 0 0 1 2 输出样例: 在这里给出相应的输出。例如: Center:(0,0) radius=1 height:2 BasalArea:3.14159 SupfaceArea:18.8496 Volume:6.28319
时间: 2024-02-09 22:12:47 浏览: 120
下面是实现以上要求的代码:
```c++
#include <iostream>
#include <cmath>
using namespace std;
const double pi = 3.14159;
class CPoint {
public:
double x;
double y;
CPoint(double _x = 0, double _y = 0) : x(_x), y(_y) {}
CPoint(const CPoint& p) {
this->x = p.x;
this->y = p.y;
}
virtual double GetArea() const = 0;
virtual double GetVolume() const = 0;
virtual void print() const {
cout << "Center:(" << x << "," << y << ")";
}
};
class CCircle : public CPoint {
public:
double radius;
CCircle(double _x = 0, double _y = 0, double _r = 0) : CPoint(_x, _y), radius(_r) {}
CCircle(const CCircle& c) : CPoint(c) {
this->radius = c.radius;
}
virtual double GetArea() const {
return pi * radius * radius;
}
virtual double GetVolume() const {
return 0;
}
virtual void print() const {
CPoint::print();
cout << " radius=" << radius;
}
};
class CCylinder : public CCircle {
public:
double height;
CCylinder(double _x = 0, double _y = 0, double _r = 0, double _h = 0) : CCircle(_x, _y, _r), height(_h) {}
CCylinder(const CCylinder& c) : CCircle(c) {
this->height = c.height;
}
virtual double GetArea() const {
return CCircle::GetArea() * 2 + 2 * pi * radius * height;
}
virtual double GetVolume() const {
return CCircle::GetArea() * height;
}
virtual void print() const {
CCircle::print();
cout << " height:" << height << " BasalArea:" << CCircle::GetArea() << " SupfaceArea:" << GetArea() << " Volume:" << GetVolume() << endl;
}
};
int main() {
double x, y, r, h;
cin >> x >> y >> r >> h;
CCircle c(x, y, r);
CCylinder cl(x, y, r, h);
c.print();
cout << endl;
cout << "BasalArea:" << c.GetArea() << endl;
cl.print();
return 0;
}
```
输出结果:
```
Center:(0,0) radius=1
BasalArea:3.14159
Center:(0,0) radius=1 height:2 BasalArea:3.14159 SupfaceArea:18.8496 Volume:6.28319
```
阅读全文