编写程序,实现点(Point)-圆(Circle)-圆柱体(Cylinder)继承体系。要求如下: (1 Pin类包含x y坐标;(2)Circl类包括凰心和半径,能计算圆的面积;《3)Cyinder类包括府面圆和隔高,能计算体积;〈4)各类要实现怡当的的造函数;(5)利用主函数进行测试。
时间: 2024-02-27 16:56:26 浏览: 15
以下是实现点(Point)-圆(Circle)-圆柱体(Cylinder)继承体系的程序,包括各类的构造函数和计算面积/体积的方法以及主函数进行测试:
```cpp
#include<iostream>
#include<cmath>
using namespace std;
class Point{
protected:
double x, y;
public:
Point(double x=0, double y=0):x(x), y(y){}
double getX(){return x;}
double getY(){return y;}
};
class Circle:public Point{
protected:
double r;
public:
Circle(double a=0, double b=0, double r=0):Point(a, b), r(r){}
double getR(){return r;}
double getArea(){return 3.14*r*r;}
};
class Cylinder:public Circle{
private:
double h;
public:
Cylinder(double a=0, double b=0, double r=0, double h=0):Circle(a, b, r), h(h){}
double getH(){return h;}
double getVolume(){return getArea()*h;}
};
int main(){
Circle c(1, 2, 3);
Cylinder cy(1, 2, 3, 4);
cout << "The area of the circle is: " << c.getArea() << endl;
cout << "The volume of the cylinder is: " << cy.getVolume() << endl;
return 0;
}
```
输出结果为:
```
The area of the circle is: 28.26
The volume of the cylinder is: 113.04
```
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)