# C++由圆派生球，圆柱，圆锥

设计的一个基类Circle类，再分别派生出圆球类Ball，圆锥类Taper 圆柱类Cylinder使用相关编程题方法求出圆的面积，其他几何图形的表面积和体积

```#include <iostream>
#include <cmath>
using namespace std;
const long double PI = 3.1415926;
class Circle
{
public:
virtual long double area() = 0;//获得表面积
virtual long double volume() = 0;//获得体积
};
//球
class Ball:public Circle
{
public:
Ball(long double radius):m_radius(radius){}
long double volume(){
return 4.0/3.0 * PI * m_radius * m_radius * m_radius;
}
long double area(){
return 4 * PI * m_radius * m_radius;
}
private:
long double m_radius;        //球的半径
};
//圆柱
class Cylinder:public Circle{
public:
Cylinder(long double radius, long double high):m_radius(radius),m_high(high){}
long double volume(){
return PI * m_radius * m_radius * m_high;
}
long double area(){
return PI * m_radius * m_radius + 2.0 * PI * m_radius * m_high;
}
private:
long double m_radius;   //圆柱的半径
long double m_high;     //圆柱的高
};
//圆锥
class Taper :public Circle
{
public:
Taper (long double radius, long double high):m_radius(radius),m_high(high){
this->m_coneElement = sqrt(m_high * m_high + m_radius * m_radius);
}
long double volume(){
return 1.0/3.0 * PI * m_radius * m_radius * m_high;
}
long double area(){
return PI * m_radius * (m_radius + m_coneElement);
}
private:
long double m_radius;//圆锥的半径
long double m_high;  //圆锥的高
long double m_coneElement;  //圆锥的母线长

};

int main()
{
Circle *p = NULL;
p = new Taper (3,4);
cout << "圆锥(3,4)的表面积是：" << p->area() << " 体积是：" << p->volume() << endl;
delete p;
p = NULL;
//
p = new Cylinder(6,4);
cout << "圆柱(6,4)的表面积是：" << p->area() << " 体积是：" << p->volume() << endl;
delete p;
p = NULL;
//
p = new Ball(6);
cout << "球(6)的表面积是：" << p->area() << " 体积是：" << p->volume() << endl;
delete p;
p = NULL;
//
return 0;
}```

相关文章
﻿

﻿