about summary refs log tree commit diff
path: root/usth/ICT2.2/labwork/5/virtual.cc
blob: 23ae78a06263650033c282cf15b4d8f46fdf0684 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <cmath>
#include <iostream>
#include <memory>
#include <string>
#include <vector>

using namespace std;

static constexpr double PI = acos (-1.0);

class Shape
{
public:
  virtual string get_name () const = 0;

  virtual double cal_area () const { return 0.0; }
  virtual double cal_vol () const { return 0.0; }
};

class Point : public Shape
{
public:
  double x, y;
  Point (double a, double b) : x {a}, y {b} {}

  string get_name () const override { return "Point"; }
};

class Circle : public Shape
{
public:
  double x, y, r;
  Circle (double a, double b, double c) : x {a}, y {b}, r {c} {}

  string get_name () const override { return "Circle"; }
  double cal_area () const override { return r * r * PI; }
};

class Cylinder : public Shape
{
public:
  double x, y, r, h;
  Cylinder (double a, double b, double c, double d)
    : x {a}, y {b}, r {c}, h {d} {}

  string get_name () const override { return "Cylinder"; }
  double cal_area () const override { return (r + h) * r * PI * 2.0; }
  double cal_vol () const override { return r * r * h * PI; }
};

int
main ()
{
  vector<unique_ptr<Shape>> v;
  v.push_back (make_unique<Point> (1.0, 2.0));
  v.push_back (make_unique<Circle> (3.0, 4.0, 5.0));
  v.push_back (make_unique<Cylinder> (6.0, 7.0, 8.0, 9.0));
  for (auto& p : v)
    cout << p->get_name () << " of area " << p->cal_area ()
         << " and volume " << p->cal_vol () << endl;
}