5.8

发布时间 2023-05-08 22:29:17作者: Code13
 1 #include<iostream>
 2 using namespace std;
 3 class Shape
 4 {
 5 public:
 6     Shape(){}
 7     ~Shape(){}
 8     virtual float getArea()=0;
 9     virtual float getPerim()=0;
10 };
11 class Circle:public Shape
12 {
13 private:
14     float itsRadius;
15 public:
16     Circle(float radius):itsRadius(radius){}
17     ~Circle(){}
18     float getArea() {return 3.14*itsRadius*itsRadius;}
19     float getPerim() {return 6.28*itsRadius;}
20 };
21 class Rectangle:public Shape
22 {
23 private:
24     float itsWidth;
25     float itsLength;
26 public:
27     Rectangle(float len,float width):itsLength(len),itsWiAdth(width){};
28     ~Rectangle(){};
29     virtual float getArea(){return itsLength*itsWidth;}
30     float getPerim() {return 2*itsLength+2*itsWidth;}
31     
32     
33     float GetLength(){return itsLength;}
34     float GetWidth(){return itsWidth;}
35 };
36 int main()
37 {
38     Shape *sp;
39     sp=new Circle(5);
40     cout<<"The area if the Circle is"<<sp->getArea()<<endl;
41     cout<<"The perimeter of the Circle is"<<sp->getPerim()<<endl;
42     delete sp;
43     sp=new Rectangle(4,6);
44     cout<<"The area of the Rectangle is"<<sp->getArea()<<endl;
45     cout<<"The perimeter of the Rectangle is"<<sp->getPerim()<<endl;
46     delete sp;
47     return 0;
48 }