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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
| #include <iostream>
#include <list>
#include <memory>
#include <algorithm>
enum class Type
{
Ship,
Car,
Plane
};
class CVehicle
{
public:
struct Coords
{
Coords(int x_, int y_):x(x_), y(y_)
{
}
Coords():Coords(0, 0)
{
}
int x, y;
};
virtual ~CVehicle()
{
}
const Coords& get_coords() const { return current_coords; }
void set_coords(const Coords& c) { current_coords = c; }
void set_coords(const int x, const int y) { set_coords(Coords(x, y)); }
const double get_amount() const { return amount; }
const size_t get_speed() const { return max_speed; }
const size_t get_year() const { return year; }
virtual Type get_type() const = 0;
protected:
CVehicle(const Coords& coords, const double am, const size_t speed, const size_t y):
current_coords(coords), amount(am), max_speed(speed), year(y)
{
}
Coords current_coords;
double amount;
size_t max_speed;
size_t year;
};
class IPassNumber
{
public:
IPassNumber(const size_t pass_n):pass_no(pass_n)
{
}
virtual ~IPassNumber()
{
}
const size_t get_pass_no() const { return pass_no; }
private:
size_t pass_no;
};
class CPlane : public CVehicle, public IPassNumber
{
public:
CPlane
(
const Coords& coords, const double amount, const size_t speed, const size_t year,
const size_t h, const size_t pass_no
):CVehicle(coords, amount, speed, year), IPassNumber(pass_no), height(h)
{
}
CPlane
(
const double amount, const size_t speed, const size_t year,
const size_t h, const size_t pass_no
):CPlane(Coords(), amount, speed, year, h, pass_no)
{
}
virtual ~CPlane()
{
}
const size_t get_height() const { return height; }
virtual Type get_type() const { return Type::Plane; }
private:
size_t height;
};
class CShip: public CVehicle, public IPassNumber
{
public:
CShip
(
const Coords& coords, const double amount, const size_t speed, const size_t year,
const size_t pass_no, const std::string& p
):CVehicle(coords, amount, speed, year), IPassNumber(pass_no), port(p)
{
}
CShip
(
const double amount, const size_t speed, const size_t year,
const size_t pass_no, const std::string& p
):CShip(Coords(), amount, speed, year, pass_no, p)
{
}
virtual ~CShip()
{
}
const std::string& get_port() const { return port; }
virtual Type get_type() const { return Type::Ship; }
private:
std::string port;
};
class CCar : public CVehicle
{
public:
CCar
(const Coords& coords, const double amount, const size_t speed, const size_t year):
CVehicle(coords, amount, speed, year)
{
}
CCar(const double amount, const size_t speed, const size_t year):
CCar(Coords(), amount, speed, year)
{
}
virtual ~CCar()
{
}
virtual Type get_type() const { return Type::Car; }
};
typedef std::shared_ptr<CVehicle> base_t;
int main()
{
std::list<base_t> lst =
{
base_t(new CCar(100, 300, 1990)),
base_t(new CShip(1000, 200, 1990, 1000, "MOW")),
base_t(new CPlane(1000, 200, 1990, 10000, 500))
};
std::for_each(lst.begin(), lst.end(), [](const base_t& value)
{
const CVehicle::Coords& coords = value->get_coords();
std::cout << "Coords: " << coords.x << " " << coords.y << std::endl;
std::cout << "Speed: " << value->get_speed()
<< " Amount: " << value->get_amount()
<< " Year: " << value->get_year() << std::endl;
});
std::list<base_t>::iterator iter = lst.begin();
++iter;
CShip* ship = dynamic_cast<CShip*>(iter->get());
std::cout << "Pass no: " << ship->get_pass_no() << " Port: " << ship->get_port() << std::endl;
++iter;
CPlane* plane = dynamic_cast<CPlane*>(iter->get());
std::cout << "Pass no: " << plane->get_pass_no() << " Height: " << plane->get_height() << std::endl;
} |