分类: C/C++
2008-05-27 11:05:26
题目:
写一个程序,定义抽象基类Shape,由它派生出5个派生类:Cricle(圆形 , Square(正方形 , Rectangle (矩形), Trapezoid (梯形), Triangle(三角形).用虚函数分别计算几重图形面积,并求它们的和.要求用基类指针数组,使它的每一个元素指向一个派生类对象.
希望朋友们能帮我改进程序哦!!~
程序:
#include
#include
using namespace std;
class shape /*抽象基类*/
{
public:
virtual float area()const {return 0;}
virtual void shapename() const=0;
virtual void show() const=0;
};
class circle:public shape /*派生类圆*/
{
private: float r;
float x,y;
public:circle(){r=0;x=0;y=0;}
circle(float a,float b,float c){r=a;x=b;y=c;}
float area() const
{
return (3.14*r*r);
}
void shapename() const
{
cout<<"shapename:circle"<
}
void show() const /*输出圆信息*/
{
shapename();
cout<<"r:"<
};
class Square:public circle /*派生类正方形*/
{
private:
float a;
public:
Square(){a=0;}
Square(float b){a=b;}
float area() const
{
return (a*a);
}
void shapename()const
{
cout<<"shapename:Square"<
void show()const /*输出正方形信息*/
{
shapename();
cout<<"a:"< cout<<"area:"< }
};
class Rectangle:public Square /*派生类矩形*/
{
private:
float a,b;
public:
Rectangle(){a=0;b=0;}
Rectangle(float c,float d){a=c;b=d;}
float area()const
{
return (a*b);
}
void shapename() const
{
cout<<"shapename:Rectangle"<
void show()const /*输出巨型信息*/
{
shapename();
cout<<"a:"< cout<<"b:"< cout<<"area:"< }
};
class Trapezoid:public shape /*派生类梯形*/
{
private:
float a,b,h;
public:
Trapezoid(){a=0;b=0;h=0;}
Trapezoid(float c,float d,float e){a=c;b=d;h=e;}
float area()const
{
return ((a+b)*h)/2;
}
void shapename()const
{
cout<<"shapename:Trapezoid"<
void show()const /*输出梯形信息*/
{
shapename();
cout<<"a:"< cout<<"b:"< cout<<"h:"<
};
class Triangle:public shape /*派生类三角形*/
{
private:
float a,b,c;
public:
Triangle(){a=0;b=0;c=0;}
Triangle(float d,float e,float f){a=d;b=e;c=f;}
float area()const /*求三角形面积*/
{
float S;
float s=(a+b+c)/2;
S=sqrt(s*(s-a)*(s-b)*(s-c));
return S;
}
void shapename()const
{
cout<<"shapename: Triangle"<
void show()const /*输出三角形三边长度及面积大小*/
{
shapename();
cout<<"a:"< cout<<"b:"< cout<<"c:"<
int Istriangle(); /*判断是否是三角形*/
};
int Triangle::Istriangle()
{
if((a+b)>c&&(b+c)>a&&(a+c)>b) /*使用两边之和大于第三边判断*/
{
cout<<"a="< return 1;
}
else
{
cout<<"输入的边长不符合三角形条件"<
}
}
int main()
{int i;
shape *p[5];
for(i=0;i<5;i++)
p[i]=NULL;
float a,b,c;
do{
cout<<"\t\t*************************************"<
switch(i)
{
case 1: {
cout<<"请输入圆半径R,横坐标X,纵坐标Y:";
cin>>a>>b>>c;
circle C(a,b,c);
p[0]=&C;
p[0]->show();
}break;
case 2: {
cout<<"请输入正方形的边长A:";
cin>>b;
Square S(b);
p[1]=&S;
p[1]->show();
}break;
case 3:{
cout<<"请输入长方形的长和宽A,B:";
cin>>a>>b;
Rectangle R(a,b);
p[2]=&R;
p[2]->show();
}break;
case 4:{
cout<<"请输入梯形上底,下底,高A,B,H:";
cin>>a>>b>>c;
Trapezoid Tra(a,b,c);
p[3]=&Tra;
p[3]->show();
}break;
case 5:{
cout<<"请输入三角形的三边A,B,C:";
cin>>a>>b>>c;
Triangle Tri(a,b,c);
p[4]=&Tri;
if(Tri.Istriangle()) /*判断是否符合三角形三边的条件*/
p[4]->show();
}break;
case 6:{float k=0;
for(i=0;i<5;i++)
{if(p[i]==NULL)
k=k+0;
else k=k+p[i]->area();}
cout<<"总面积为:"<
break;
}
}while(i!=0);
return 0;
}