/*
* 程序的版权和版本声明部分
* Copyright (c)2012, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称:abc.cpp
* 作 者:杨蕾
* 完成日期: 2013 年 6 月 6日
* 版本号: v1.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
*/
#include <iostream>
using namespace std;
const double pai=3.1415926;
//抽象立体图形基类
class CSolid
{
public:
virtual double Surfacearea() const=0;
virtual double Volume() const=0;
};
//立方体类
class CCube:public CSolid
{
public:
CCube(double len=0);
double Surfacearea() const;
double Volume() const;
private:
double length;
};
CCube::CCube(double len)//立方体类构造函数
{
length=len;
}
double CCube::Surfacearea() const//求立方体的表面积
{
return 6*length*length;
}
double CCube::Volume() const//求立方体的体积
{
return length*length*length;
}
class CBall:public CSolid
{
private:
double radius;//圆周率
public:
CBall(double r=0);
double Surfacearea() const;
double Volume() const;
};
CBall::CBall(double r)
{
radius=r;
}
double CBall::Surfacearea() const
{
return 4*pai*radius*radius;
}
double CBall::Volume() const
{
return (4/3)*pai*radius*radius*radius;
}
class Ccylinder:public CSolid
{
private:
double height;
double radius;
public:
Ccylinder (double hei=0,double r=0);
double Surfacearea() const;
double Volume() const;
};
Ccylinder::Ccylinder(double hei,double r)
{
height=hei;
radius=r;
}
double Ccylinder::Surfacearea() const
{
return 2*pai*radius*radius+2*pai*radius*height;
}
double Ccylinder::Volume() const
{
return pai*radius*radius*height;
}
int main( )
{
CSolid *p;
double s,v;
CCube x(10);
cout<<"立方体边长为 10 "<<endl;
p=&x;
s=p->Surfacearea( );
v=p->Volume( );
cout<<"表面积:"<<s<<endl;
cout<<"体积:"<<v<<endl;
cout<<endl;
CBall y(3.6);
cout<<"球体半径为 3.6 "<<endl;
p=&y;
s=p->Surfacearea( );
v=p->Volume( );
cout<<"表面积:"<<s<<endl;
cout<<"体积:"<<v<<endl;
cout<<endl;
Ccylinder z(12,16);
cout<<"圆柱体底面半径、高分别为 12, 16"<<endl;
p=&z;
s=p->Surfacearea( );
v=p->Volume( );
cout<<"表面积:"<<s<<endl;
cout<<"体积:"<<v<<endl;
cout<<endl;
return 0;
}
运行结果: