1 | #include <bits/stdc++.h> |
2 | |
3 | using namespace std; |
4 | |
5 | const double PI = 3.14159265358979323846; |
6 | |
7 | class Shape { |
8 | public: |
9 | |
10 | virtual double getArea() = 0; |
11 | }; |
12 | |
13 | class Circle : public Shape { |
14 | public: |
15 | double radius; |
16 | |
17 | Circle(double r) { |
18 | radius = r; |
19 | } |
20 | |
21 | double getArea() override { |
22 | return PI * radius; |
23 | } |
24 | }; |
25 | |
26 | class Rectangle : public Shape { |
27 | public: |
28 | double width, height; |
29 | |
30 | Rectangle(double w, double h) { |
31 | width = w; |
32 | height = h; |
33 | } |
34 | |
35 | double getArea() override { |
36 | return width * height; |
37 | } |
38 | }; |
39 | |
40 | class Triangle : public Shape { |
41 | public: |
42 | double a, b, c; |
43 | |
44 | Triangle(double x, double y, double z) { |
45 | a = x; |
46 | b = y; |
47 | c = z; |
48 | } |
49 | |
50 | double getArea() override { |
51 | double p = (a + b + c) / 2; |
52 | return sqrt(p * (p - a) * (p - b) * (p - c)); |
53 | } |
54 | }; |
55 | |
56 | int main() { |
57 | Circle circle = Circle(1); |
58 | Rectangle rect = Rectangle(2, 5); |
59 | Triangle tr = Triangle(3, 4, 5); |
60 | cout << fixed << setprecision(3); |
61 | cout << circle.getArea() << " " << rect.getArea() << " " << tr.getArea() << endl; |
62 | return 0; |
63 | } |