#include <iostream>
using namespace std;
class Shape {
public:
virtualdouble getArea() const =0;
virtualvoid print() const =0;
virtual~Shape(){}
};
class Circle : public Shape {
public:
Circle(int = 0, int = 0, double = 0.0 );
virtualdouble getArea() const; // 返回面积
virtualvoid print() const; // 输出Circle 类对象t
private:
intx,y; // 圆心座标
doubleradius; //圆半径
}; //派生类Circle定义结束
class Rectangle : public Shape {
public:
Rectangle(int = 0, int = 0); // 构造函数
virtualdouble getArea() const; // 返回面积
voidprint() const; // 输出Rectangle类对象
protected:
inta,b; // 矩形的长和宽
}; //派生类
double Shape::getArea() const
{
cout<<"基类的getArea函数,面积是 ";
return 0.0;
} //Shape类getArea函数的定义
class Delta: public Shape{
public:
Delta(double =0,double =0);
doublegetArea() const;
voidprint() const;
private:
doubled,h;
};
void Shape::print() const
{
cout<<"Baseclass Object"<<endl;
} //Shape类print函数定义
Circle::Circle( int xValue, int yValue,double radiusValue )
{
x=xValue; y=yValue;
radius=radiusValue ;
} //Circle类构造函数
double Circle::getArea() const
{
cout<<"Circle类的getArea函数,面积是 ";
return 3.14159 * radius * radius;
} //Circle类getArea函数定义
void Circle::print() const
{
cout << "center is ";
cout<<"x="<<x<<" y="<<y;
cout << "; radius is " << radius<<endl;
} //Circle类print函数定义
Rectangle::Rectangle( int aValue, intbValue )
{
a=aValue; b=bValue;
} //Rectangle类构造函数
double Rectangle::getArea() const
{
cout<<"Rectangle类的getArea函数,面积是";
return a * b;
} // Rectangle类getArea函数定义
void Rectangle::print() const
{
cout << "hight is "<<a;
cout<<"width is"<<b<<endl;
}
Delta::Delta( double a, double b )
{
d=a; h=b;
} //Delta类构造函数
double Delta::getArea() const
{
cout<<"Delta类的getArea函数,面积是 ";
return ((1.0/2)*d * h);
} // Delta类getArea函数定义
void Delta::print() const
{
cout << "dibian is "<<d;
cout<<"high is"<<h<<endl;
} //Delta类print函数定义
class Cube : public Rectangle { //派生类Cube的定义
public:
Cube(intx=0, int y=0, int z=0):Rectangle(x,y),c(z){};
doublegetArea() const;
voidprint() const;
private:
intc;
};
double Cube::getArea() const
{return a*b*c;}
void Cube::print() const
{
cout<<"Cube:h="<<c<<",length="<<a
<<",width="<<b<<",Area="<<a*b*c<<endl;
}
void creat_object(Shape **ptr);
void display_area(Shape *ptr);
void delete_object(Shape *ptr);
int main()
{
Shape *shape_ptr;
creat_object(&shape_ptr);
display_area(shape_ptr);
delete_object(shape_ptr);
return0;
}
void creat_object(Shape **ptr)
{ chartype;
*ptr= NULL; //空指针
do{
cout<<"创建对象。请选择:";
cout<<"c:Circle类对象;r:Rectangle类对象;s:Delta类对象;u:Cube类对象"<<endl;
cin>>type;
switch (type)
{case 'c': //创建Ciecle类对象
{intxx,yy;
double rr;
cout<<"请输入圆心的座标和圆的半径:";
cin>>xx>>yy>>rr;
*ptr = new Circle(xx,yy,rr);
break;
}
case 'r': //创建Rectangle类对象
{intaa,bb;
cout<<"请输入矩形的长和宽:";
cin>>aa>>bb;
*ptr = new Rectangle(aa,bb);
break;
}
case 's': //创建Delta类对象
{
double dd,hh;
cout<<"请输入三角形的底和高:";
cin>>dd>>hh;
*ptr= new Delta(dd,hh);
break;
}
case 'u': //创建Cube类对象
{intaa,bb,cc;
cout<<"请输入立方体的长、宽、高:";
cin>>aa>>bb>>cc;
*ptr = new Cube(aa,bb,cc);
break;
}
default:cout<<"类型错误,请重新选择\n";
}
}while(*ptr==NULL);
}
void display_area(Shape *ptr)
{ cout<<"显示所创建对象的面积,调用的是"<<endl;
cout<<ptr->getArea() << endl;
}
void delete_object(Shape *ptr)
{ delete(ptr);
}