希望你有用。
#include
class Sharp
{
public:
Sharp(){}
~Sharp(){}
virtual float GetArea()=0;
virtual float GetPerim()=0;
};
class Rectangle:public Sharp
{
public:
Rectangle(float len,float width):itsLength(len),itsWidth(width){};
~Rectangle();
virtual float GetArea(){return itsLength*itsWidth;}
float GetPerim(){return 2*itsLength+2*itsWidth;}
virtual float GetLength(){return itsLength;}
virtual float GetWidth(){return itsWidth;}
private:
float itsLength;
float itsWidth;
};
class Circle:public Sharp
{
public:
Circle(float radius):itsRadius(radius){}
~Circle(){}
float GetArea(){return 3.14*itsRadius*itsRadius;}
float GetPerim(){return 6.28*itsRadius;}
private:
float itsRadius;
};
void main()
{
Sharp *sp;
sp=new Circle(5);
cout<<"The area of the Circle is"<
sp=new Rectangle(4,6);
cout<<"The area of the Rectangle is"<
}
。。。。。。。。
声明一个Shape类,具有纯虚函数getArea和getPerim,Rectangle和Circle的数据成员自己随便添加。
然后就可以了撒。很简单。
class Sharp
{
public:
Sharp(double area,double perim)
:Area(area),Perim(perim){}
virtual float GetArea()=0;
virtual float GetPerim()=0;
!Sharp(){}
private:
double Area;
double Perim;
};