#include<iostream>
#include<Cmath>
using namespace std;
const double pi=3.1415926;
class CSolid //定义抽象基类
{
public:
virtual double superficial_areas()=0;//表面积
virtual double volume()=0; //体积
};
class CCube: public CSolid //利用抽象基类定义正方体类
{
public:
CCube(double CCube_long);//构造函数
~CCube(){}
double superficial_areas();
double volume();
private:
double CCube_long;
};
class CBall: public CSolid //利用抽象基类定义球体类
{
public:
CBall(double CBall_long);//构造函数
~CBall(){}
double superficial_areas();
double volume();
private:
double CBall_long;
};
class CCylinder: public CSolid //利用抽象基类定义圆柱体类
{
public:
CCylinder(double CCylinder_bottom,double CCylinder_high); //构造函数
~CCylinder(){}
double superficial_areas();
double volume();
private:
double CCylinder_bottom;
double CCylinder_high;
};
CCube::CCube(double CCube_long)//构造函数
{
this->CCube_long=CCube_long;
}
double CCube::superficial_areas()
{
return (6*CCube_long*CCube_long);
}
double CCube::volume()
{
return (CCube_long*CCube_long*CCube_long);
}
CBall::CBall(double CBall_long)
{
this->CBall_long=CBall_long;
}
double CBall::superficial_areas()
{
return (4*pi*CBall_long*CBall_long);
}
double CBall::volume()
{
return (4*pi*CBall_long*CBall_long*CBall_long/3);
}
CCylinder::CCylinder(double CCylinder_bottom,double CCylinder_high)
{
this->CCylinder_bottom=CCylinder_bottom;
this->CCylinder_high=CCylinder_high;
}
double CCylinder::superficial_areas()
{
return (2*pi*CCylinder_bottom*CCylinder_bottom+2*pi*CCylinder_bottom*CCylinder_high);
}
double CCylinder::volume()
{
return (pi*CCylinder_bottom*CCylinder_bottom*CCylinder_high);
}
int main()
{
CCube CCube1(12.6); //建立CCube类对象c1,参数为正方体边长
CBall CBall1(4.5); //建立CBall1类对象CBall1,参数为球的半径
CCylinder CCylinder1(4.5,8.4); //建立CCylinder类对象CCylinder1,参数为圆柱体的半径和高
CSolid *p;
p=&CCube1;
cout<<"totol of CCube areas="<<p->superficial_areas()<<endl; //输出表面积
cout<<"totol of CCube volume="<<p->volume()<<endl; //输出体积
p=&CBall1;
cout<<"totol of CBall areas="<<p->superficial_areas()<<endl; //输出表面积
cout<<"totol of CBall volume="<<p->volume()<<endl; //输出体积
p=&CCylinder1;
cout<<"totol of CCylinder areas="<<p->superficial_areas()<<endl; //输出表面积
cout<<"totol of CCylinder volume="<<p->volume()<<endl; //输出体积
system("pause");
return 0;
}