已知Point(点)类,点类的公有派生类Circle(圆)类,圆类的公有派生Cylinder(圆柱体)类如下: class Point { public: virtual double area() { return 0; } }; class Circle:public Point { protected: double m_radius; public: Circle(double r); virtual double area(); }; class Cylinder:public Circle { private: double m_high; public: Cylinder(double r, double h); virtual double area(); }; 试完成成员函数的定义,在主函数中输入圆的半径和圆柱的高,利用指向基类的指针,分别输出圆的面积和圆柱体的表面积。 Input 2个数,分别代表圆的半径和圆柱的高。 Output 圆的面积和圆柱体的表面积(取2位小数)。 Sample Input 1 1.5 2 Sample Output 1 7.06 32.97用c++写出此代码
时间: 2024-02-03 14:02:07 浏览: 72
```
#include <iostream>
#include <iomanip>
using namespace std;
class Point {
public:
virtual double area() { return 0; }
};
class Circle : public Point {
protected:
double m_radius;
public:
Circle(double r) : m_radius(r) {}
virtual double area() { return 3.14 * m_radius * m_radius; }
};
class Cylinder : public Circle {
private:
double m_high;
public:
Cylinder(double r, double h) : Circle(r), m_high(h) {}
virtual double area() { return 2 * Circle::area() + 2 * 3.14 * m_radius * m_high; }
};
int main() {
double r, h;
cin >> r >> h;
Point* p1 = new Circle(r);
Point* p2 = new Cylinder(r, h);
cout << fixed << setprecision(2) << p1->area() << " " << p2->area() << endl;
delete p1;
delete p2;
return 0;
}
```
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)