5.10

 1 #include<iostream>
 2 using namespace std;
 3 class Shape
 4 {
 5 public:
 6     Shape(){}
 7     ~Shape(){}
 8     virtual float getArea() {}
 9 };
10 class Circle:public Shape
11 {
12 private:
13     float itsRadius;
14 public:
15     Circle(float radius):itsRadius(radius){}
16     ~Circle(){}
17     float getArea()
18     {
19         return 3.14*itsRadius*itsRadius;
20     }
21 };
22 class Rectangle:public Shape
23 {
24 private:
25     float itsWidth;
26     float itsLength;
27 public:
28     Rectangle(float len,float width):itsLength(len),itsWidth(width){}
29     ~Rectangle(){}
30     float getArea()
31     {
32         return itsLength*itsWidth;
33     }
34 };
35 class Square:public Rectangle
36 {
37 public:
38     Square(float len):Rectangle(len,len){}
39     ~Square(){}
40 };
41 int main()
42 {
43     Shape*sp;
44     sp=new Circle(5);
45     cout<<"the area of the Circle is"<<sp->getArea()<<endl;
46     delete sp;
47     sp=new Rectangle(4,6);
48     cout<<"the area of the Rectangle is"<<sp->getArea()<<endl;
49     delete sp;
50     sp=new Square(5);
51     cout<<"the area of the Square is"<<sp->getArea()<<endl;
52     delete sp;
53 }

 

posted @ 2023-05-10 18:45  Code13  阅读(24)  评论(0编辑  收藏  举报