编写一个C++程序计算圆、圆锥、圆柱的表面积和体积。

计算它们都需要圆的半径,所以要定义一个圆类,它包含表示半径的数据成员;圆柱类和圆锥类委员的派生类,通过继承获得圆的半径。

第1个回答  2010-05-20
#include<iostream.h>
#define pi 3.14
class Shape
{
public:
double area()
{
return a;
}
double grith()
{
return g;
}

protected:
double a;
double g;
};

class Cricle:public Shape
{
public:
double area(double x=2)
{
a=pi*x*x;
return a;
}
double grith(double x=2)
{
g=2*pi*x;
return g;

}
show()
{
cout<<"Shape Opject:"<<endl;
cout<<"Cricle"<<endl;
cout<<"area:"<<area()<<endl;
cout<<"grith:"<<grith()<<endl;
}

protected:
double g;
double a;
};
class Triangle:public Shape
{
public:
double area(double x=1,double y=2,double z=3)
{
a=0.5*x*y;
return a;
}
double grith(double x=1,double y=2,double z=3)
{
g=x+y+z;
return g;

}
show()
{
cout<<"Shape Opject:"<<endl;
cout<<"Tritangle"<<endl;
cout<<"area:"<<area()<<endl;
cout<<"grith:"<<grith()<<endl;
}
protected:
double a;
double g;
};
class Rectangle:public Shape
{
public:
double area(double x=1,double y=2)
{
a=x*y;
return a;
}
double grith(double x=1,double y=2)
{
g=2*(x+y);
return g;

}
show()
{
cout<<"Shape Opject:"<<endl;
cout<<"Tritangle"<<endl;
cout<<"area:"<<area()<<endl;
cout<<"grith:"<<grith()<<endl;
}
protected:
double a;
double g;
};

void main()
{
Cricle A;

A.show();
Triangle B;

B.show();
Rectangle C;

C.show();
}本回答被提问者采纳
相似回答