-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHouse.cpp
214 lines (171 loc) · 3.79 KB
/
House.cpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <iostream>
#include <vector>
using namespace std;
class Pet
{
protected:
string name;
int code;
public:
Pet(){}
virtual ~Pet() {}
virtual void make_noise()
{
cout << "rrrrrrrr" << endl;
}
string get_name()
{
return this->name;
}
bool operator==(Pet other)
{
if (other.name!=this->name) return false;
else return true;
}
};
class Dog : public Pet
{
public:
Dog(){}
Dog(string name, int code)
{
this->name=name;
this->code=code;
}
void make_noise()
{
cout << "gau gau" << endl;
}
};
class Cat : public Pet
{
public:
Cat(){}
Cat(string name, int code)
{
this->name=name;
this->code=code;
}
void make_noise()
{
cout << "meo meo" << endl;
}
};
class Address
{
protected:
string street;
int number;
string city;
public:
Address(){}
Address(string street, int number, string city)
{
this->street = street;
this->number = number;
this->city = city;
}
void print_info()
{
cout << this->street << endl;
cout << this->number << endl;
cout << this->city << endl;
}
string get_street()
{
return this->street;
}
bool operator==(Address other)
{
if (other.street!=this->street) return false;
return true;
}
};
class House
{
private:
Address address;
vector <Pet*> pets;
public:
House() {}
House(Address address)
{
this->address = address;
}
void add_pets(Pet* pet)
{
pets.push_back(pet);
}
void remove_pets(Pet pet)
{
for (int i=0;i<pets.size();i++)
{
Pet *n=dynamic_cast<Pet*>(this->pets[i]);
if (n)
{
if (*n==pet)
pets.erase(pets.begin()+i);
}
}
}
void print_pets()
{
for(int i = 0; i < pets.size(); i++)
{
cout << pets[i]->get_name() << endl;
}
}
string get_street()
{
return address.get_street();
}
int number_pets()
{
return pets.size();
}
};
class Neighborhood
{
private:
vector <House*> list_of_house;
public:
void add_house(House* house)
{
list_of_house.push_back(house);
}
void count_pets_same_street(string street)
{
int sum = 0;
for(int i = 0; i < list_of_house.size(); i++)
{
if(list_of_house[i]->get_street() == street)
{
sum += list_of_house[i]->number_pets();
}
}
cout << sum << endl;
};
};
int main()
{
Address A("abcd",100,"abc");
House a(A);
Pet *Thang = new Dog("Thang",19);
Dog Quang("Quang",20);
Dog Khang("Khang",69);
Cat Huong("Huong",50);
Pet *o = new Dog("QuangNet",20);
o->make_noise();
Address B("abcd",10,"abc");
House b(B);
Neighborhood s;
a.add_pets(Thang);
a.add_pets(&Quang);
b.add_pets(&Khang);
b.add_pets(&Huong);
b.add_pets(o);
vector<House*> list_of_house;
s.add_house(&a);
s.add_house(&b);
s.count_pets_same_street("abcd");
} list_of_hou