#include<iostream> using namespace std; class Shape{ public: Shape(); ~Shape(); virtual float GetAre

2025-01-19 07:15:55
推荐回答(1个)
回答1:

#include
using namespace std;

class Shape{
public:
Shape();
~Shape();
virtual float GetArea()=0;
virtual float GetPerim()=0;
};

class Circle:public Shape{
public:
Circle(float radius):itsRadius(radius){}
~Circle(){}
float GetArea(){return 3.14*itsRadius*itsRadius;}
float GetPerim(){return 6.28*itsRadius;}
private:
float itsRadius;
};
class Rectangle:public Shape
{
public:
Rectangle(float len,float width):itsLength(len),itsWidth(width){}
~Rectangle(){}
float GetArea(){return itsLength*itsWidth;}
float GetPerim(){return 2*itsLength*itsWidth;}
float GetLength(){return itsLength;}
float GetWidth(){return itsWidth;}
private:
float itsWidth;
float itsLength;
};
int main()
{
Shape *sp;
sp = new Circle(5);
cout<<"The area of the Circle is"<GetArea()< cout<<"The perimeter of the Circle is"<GetPerim()< delete sp;
sp=new Rectangle(4,6);//细心一点,用逗号隔开,用成了点号
cout<<"The area of the Circle is"<GetArea()< cout<<"The perimeter of the Circle is"<GetPerim()< delete sp;
return 0;
}//细心一点儿就好了,别在小细节上出错,很难发现的呀