程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2012. 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者: 程龙
* 完成日期: 2012 年 05 月 18 日
* 版 本 号:
* 对任务及求解方法的描述部分
* 输入描述:
设计一个抽象类CSolid,含有两个求表面积及体积的纯虚函数。设计个派生类CCube、CBall、CCylinder,分别表示正方体、球体及圆柱体。在main()函数中,定义基类的指针p(CSolid *p;),利用p指针,输出正方体、球体及圆柱体对象的表面积及体积。
* 问题描述:
* 程序输出:
* 程序头部的注释结束
#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(10.6); //建立CCube类对象c1,参数为正方体边长
CBall CBall1(4.7); //建立CBall1类对象CBall1,参数为球的半径
CCylinder CCylinder1(4.9,6.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;
}
11