分类: C/C++
2011-08-10 11:37:06
/*
* 课堂代码实例
*/
#include
#include
using namespace std;
#define PI 3.1415926
//enum type {cube,ball,calender};
class shape{
public:
virtual void getattr()=0
{
cout<<"基类"<<endl;
}//取得属性
virtual void setattr()=0;//设置属性
virtual void getarea()=0;//获取表面积
virtual void getvolume()=0;//获取体积
private:
};
/***********立方体类**********/
class cube:public shape{
public:
cube(double x=0,double y=0,double z=0):lenth(x)
,width(y),height(z)
{
area=2*(lenth*width+lenth*height+width*height);
volume=lenth*width*height;
}
void getattr(){
cout<<"type:"<<"cube"<<endl;
cout<<"size:"<<setw(5)<<lenth
<<width<<height<<endl;
cout<<"area:"<<area<<endl;
cout<<"volume:"<<volume<<endl;
}
void setattr(){
cout<<"type:"<<"cube"<<endl;
cout<<"请输入立方体长:"<<endl;
cin>>lenth;
cout<<"请输入立方体宽:"<<endl;
cin>>width;
cout<<"请输入立方体高:"<<endl;
cin>>height;
}
void getarea(){
area=2*(lenth*width+lenth*height+width*height);
cout<<"the cube area:"<<area<<endl;
}
void getvolume(){
volume=lenth*width*height;
cout<<"the cube volume:"<<volume<<endl;
}
private:
//enum type shape;
double lenth;
double width;
double height;
double area;
double volume;
};
/***********圆柱体类*********/
class calender:public shape{
public:
calender(double x=0,double y=0):radius(x)
,height(y)
{
area=2*radius*radius*PI+2*radius*PI*height;
volume=radius*radius*PI*height;
}
void getattr(){
cout<<"type:"<<"calender"<<endl;
cout<<"size:"<<setw(5)<<radius
<<height<<endl;
cout<<"area:"<<area<<endl;
cout<<"volume:"<<volume<<endl;
}
void setattr(){
cout<<"type:"<<"calendar"<<endl;
cout<<"请输入底面半径:"<<endl;
cin>>radius;
cout<<"请输入高:"<<endl;
cin>>height;
}
void getarea(){
area=2*radius*radius*PI+2*radius*PI*height;
cout<<"the calender area:"<<area<<endl;
}
void getvolume(){
volume=radius*radius*PI*height;
cout<<"the calender volume:"<<volume<<endl;
}
private:
//enum type type;
double radius;
double height;
double area;
double volume;
};
/***********球体类***********/
class ball:public shape{
public:
ball(double x=0):radius(x)
{
area=4*radius*radius*PI;
volume=(4*PI*radius*radius*radius)/3.0;
}
void getattr(){
cout<<"type:"<<"ball"<<endl;
cout<<"size:"<<setw(5)<<radius<<endl;
cout<<"area:"<<area<<endl;
cout<<"volume:"<<volume<<endl;
}
void setattr(){
cout<<"type:"<<"cube"<<endl;
cout<<"请输入球体半径:"<<endl;
cin>>radius;
}
void getarea(){
area=4*radius*radius*PI;
cout<<"the ball area:"<<area<<endl;
}
void getvolume(){
volume=(4*PI*radius*radius*radius)/3.0;
cout<<"the ball volume:"<<volume<<endl;
}
private:
//enum type type;
double radius;
double area;
double volume;
};
void getattr(shape *p)
{
p->shape::getattr();
p->getattr();
}
void setattr(shape *p)
{
p->setattr();
}
void getarea(shape *p)
{
p->getarea();
}
void getvolume(shape *p)
{
p->getvolume();
}
int menu()
{
int choose;
cout<<"1.创建一个立方体"<<'\n'
<<"2.创建一个圆柱体"<<'\n'
<<"3.创建一个球体"<<'\n'
<<"4.退出"<<endl;
cin>>choose;
return choose;
}
int main()
{
int choose;
cube cu;
ball ba;
calender ca;
while(1)
{
choose=menu();
switch(choose)
{
case 1:
setattr(&cu);
getattr(&cu);
getarea(&cu);
getvolume(&cu);
break;
case 2:
setattr(&ca);
getarea(&ca);
getvolume(&ca);
break;
case 3:
setattr(&ba);
getarea(&ba);
getvolume(&ba);
break;
default:
return 1;
}
}
}