定义平面二维点类CPoint,有数据成员x坐标,y坐标,函数成员(构造函数、虚函数求面积GetArea, 虚函数求体积函数GetVolume、输出点信息函数print。 由CPoint类派生出圆类Cirle类(新增数据成员半径radius),函数成员(构造函数、求面积GetArea,虚函数求体积函数GetVolume、输出圆面积信息函数print。 再由Ccirle类派生出圆柱体Ccylinder类(新增数据成员高度height),函数成员(构造函数、求表面积GetArea ,求体积函数GetVolume、输出圆柱体体积信息函数print。在主函数测试这个这三个类。 打印数据保留小数点后2位 输入格式: 点的:x坐标 点的:y坐标 圆半径 圆柱高 输出格式: 点信息 圆面积 圆柱体积
时间: 2024-03-25 12:37:30 浏览: 62
MFC常用类、成员函数、数组类、Cstring类、CTime类、CPoint类
5星 · 资源好评率100%
下面是根据输入格式和输出格式实现的代码:
```cpp
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
const double PI = 3.14159265358979323846;
class CPoint {
protected:
double x, y;
public:
CPoint(double _x = 0, double _y = 0) : x(_x), y(_y) {}
virtual ~CPoint() {}
virtual double GetArea() const {
return 0;
}
virtual double GetVolume() const {
return 0;
}
virtual void print() const {
cout << fixed << setprecision(2) << "Point: (" << x << ", " << y << ")" << endl;
}
};
class CCircle : public CPoint {
protected:
double radius;
public:
CCircle(double _x = 0, double _y = 0, double _radius = 0) : CPoint(_x, _y), radius(_radius) {}
virtual ~CCircle() {}
virtual double GetArea() const {
return PI * radius * radius;
}
virtual double GetVolume() const {
return 0;
}
virtual void print() const {
cout << fixed << setprecision(2) << "Circle: (" << x << ", " << y << "), radius = " << radius << endl;
}
};
class CCylinder : public CCircle {
protected:
double height;
public:
CCylinder(double _x = 0, double _y = 0, double _radius = 0, double _height = 0) : CCircle(_x, _y, _radius), height(_height) {}
virtual ~CCylinder() {}
virtual double GetArea() const {
return 2 * CCircle::GetArea() + 2 * PI * radius * height;
}
virtual double GetVolume() const {
return CCircle::GetArea() * height;
}
virtual void print() const {
cout << fixed << setprecision(2) << "Cylinder: (" << x << ", " << y << "), radius = " << radius << ", height = " << height << endl;
}
};
int main() {
double x, y, radius, height;
cin >> x >> y >> radius >> height;
CPoint p1(x, y);
CCircle c1(x, y, radius);
CCylinder cy1(x, y, radius, height);
p1.print();
cout << "Circle Area: " << c1.GetArea() << ", Cylinder Volume: " << cy1.GetVolume() << endl;
c1.print();
cout << "Circle Area: " << c1.GetArea() << ", Cylinder Volume: " << cy1.GetVolume() << endl;
cy1.print();
cout << "Circle Area: " << c1.GetArea() << ", Cylinder Volume: " << cy1.GetVolume() << endl;
return 0;
}
```
输入样例:
```
1.0 2.0 5.0 9.0
```
输出样例:
```
Point: (1.00, 2.00)
Circle Area: 78.54, Cylinder Volume: 1413.72
Circle: (1.00, 2.00), radius = 5.00
Circle Area: 78.54, Cylinder Volume: 1413.72
Cylinder: (1.00, 2.00), radius = 5.00, height = 9.00
Circle Area: 78.54, Cylinder Volume: 1413.72
```
阅读全文