菜鸟的博客

纵有疾风起,人生不言弃。

导航

2023.5.23

 

 1 #include <iostream>
 2 #include <iomanip>
 3 using namespace std;
 4 float PI = 3.14159f;
 5 class Shape
 6 {
 7 public:
 8     virtual float getArea() = 0;
 9 };
10 class Circle :public Shape
11 {
12 public:
13     float r;
14     float getArea()
15     {
16         return PI * r * r;
17     }
18 };
19 class Square :public Shape
20 {
21 public:
22     float x;
23     float getArea()
24     {
25         return x * x;
26     }
27 };
28 class Rectangle :public Shape
29 {
30 public:
31     float w, l;
32     float getArea()
33     {
34         return w * l;
35     }
36 };
37 class Trapezoid :public Shape
38 {
39 public:
40     float s, x, h;
41     float getArea()
42     {
43         return (s + x) * h / 2;
44     }
45 };
46 class Triangle :public Shape
47 {
48 public:
49     float d, h;
50     float getArea()
51     {
52         return (d * h) / 2;
53     }
54 };
55 void test01()
56 {
57     Circle c;
58     Square s;
59     Rectangle r;
60     Trapezoid t;
61     Triangle a;
62     cin >> c.r >> s.x >> r.w >> r.l >> t.s >> t.x >> t.h >> a.d >> a.h;
63     Shape* arr[5] = { &c,&s,&r,&t,&a };
64     float sumArea = 0;
65     for (int i = 0; i < 5; i++)
66     {
67         sumArea = sumArea + arr[i]->getArea();
68     }
69     cout <<fixed<<setprecision(3)<< sumArea << endl;
70 }
71 int main()
72 {
73     test01();
74     return 0;
75 }

 

posted on 2023-05-23 21:24  hhmzd233  阅读(10)  评论(0)    收藏  举报