/*。
*Copyright(c)2014,烟台大学计算机学院
*All right reserved,
*文件名:test.cpp
*作者:毕玉堂
*完成日期:2015年6月7日
*版本号:v1.0
*
问题描述:
*输入描述:
*程序输出:
*/
#include <iostream>
using namespace std;
class CSolid
{
public:
virtual double surface_area()=0;
virtual double volume()=0;
};
class CCube:public CSolid
{
public:
CCube(double s):side_length(s){}
virtual double surface_area()
{
return side_length*side_length*6;
}
virtual double volume()
{
return side_length*side_length*side_length;
}
private:
double side_length;
};
class CBall:public CSolid
{
public:
CBall(double r):radius(r){}
virtual double surface_area()
{
return 4*3.14*radius*radius;
}
virtual double volume()
{
return (4/3)*3.14*(radius*radius*radius);
}
private:
double radius;
};
class CCylinder:public CSolid
{
public:
CCylinder(double r,double h):radius(r),height(h){}
virtual double surface_area()
{
return 2*3.14*radius*2*height+2*3.14*radius*radius;
}
virtual double volume()
{
return 3.14*radius*radius*height;
}
private:
double radius;
double height;
};
int main()
{
CSolid *p;
double s,v;
CCube x(30);
cout<<"立方体边长为 30 "<<endl;
p=&x;
s=p->surface_area( );
v=p->volume( );
cout<<"表面积:"<<s<<endl;
cout<<"体积:"<<v<<endl;
cout<<endl;
CBall y(4.5);
cout<<"球体半径为 4.5 "<<endl;
p=&y;
s=p->surface_area( );
v=p->volume( );
cout<<"表面积:"<<s<<endl;
cout<<"体积:"<<v<<endl;
cout<<endl;
CCylinder z(10,20);
cout<<"圆柱体底面半径、高分别为 10, 20"<<endl;
p=&z;
s=p->surface_area( );
v=p->volume();
cout<<"表面积:"<<s<<endl;
cout<<"体积:"<<v<<endl;
cout<<endl;
return 0;
}
运行结果: