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
| #include <vector>
#include <iostream>
#include <algorithm>
class Room
{
friend std::ostream& operator<<(std::ostream&, const Room&);
public:
Room() {}
Room(int width, int height, int length, int windows = 0)
: _width(width), _height(height), _length(length), _windows(windows) {}
int width() const { return _width; }
int height() const { return _height; }
int length() const { return _length; }
int windows() const { return _windows; }
int square() const { return _width * _length; }
private:
int _width;
int _height;
int _length;
int _windows;
};
class Flat
{
friend std::ostream& operator<<(std::ostream&, const Flat&);
public:
Flat(int level) : _level(level) {}
Room* add_room(Room* room)
{
_rooms.push_back(room);
return _rooms.back();
}
Room* add_room(int width, int height, int length)
{
_rooms.push_back(new Room(width, height, length));
return _rooms.back();
}
void remove_room(Room *room)
{
_rooms.erase(std::remove(_rooms.begin(), _rooms.end(), room),
_rooms.end());
}
int rooms_count() const { return _rooms.size(); }
private:
int _level;
std::vector<Room*> _rooms;
};
std::ostream& operator<<(std::ostream& out, const Room& room)
{
return out << "Room's"
<< " width: " << room.width()
<< " height: " << room.height()
<< " length: " << room.length();
}
std::ostream& operator<<(std::ostream& out, const Flat& flat)
{
out << "Flat's level " << flat._level << std::endl;
out << "Contains rooms: " << std::endl;
std::vector<Room*>::const_iterator cit = flat._rooms.begin();
for ( ; cit != flat._rooms.end(); ++cit)
out << **cit << std::endl;
return out;
}
int main()
{
std::vector<Room*> other_rooms;
other_rooms.push_back(new Room(10, 10, 10));
Flat flat(2);
Room *room = flat.add_room(new Room(20, 20, 20));
other_rooms.push_back(room);
flat.add_room(other_rooms[0]);
std::cout << "My new flat is " << flat << std::endl;
std::cin.get();
return 0;
} |