-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask4.cpp
115 lines (107 loc) · 3.73 KB
/
task4.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
#include <iostream>
#include"reading.cpp"
int main()
{
reading();
std::cout << "------------------------------------------------\nThe fourth task:\n";
std::cout<<owners.size()<<" "<<anims.size()<<"\n";
int oldestAge = 0;
int youngestAge = INT_MAX;
std::vector<Animal *> youngestAnimal;
std::vector<Animal *> oldestAnimal;
for (int i = 0; i < owners.size(); ++i)
{
std::vector<Animal *> animals = owners[i].GetVectorAnimal();
for (Animal *anim : animals)
{
if (Cat *c = dynamic_cast<Cat *>(anim))
{ // динамическое приведение типов (dynamic_cast) используется для динамического приведения типов указателей или ссылок на объекты.
if (c->GetAnimalAge() == oldestAge)
{
oldestAnimal.push_back(anim);
}
else if (c->GetAnimalAge() == youngestAge)
{
youngestAnimal.push_back(anim);
}
else if (c->GetAnimalAge() < youngestAge)
{
youngestAge = c->GetAnimalAge();
youngestAnimal.clear();
youngestAnimal.push_back(anim);
}
else if (c->GetAnimalAge() > oldestAge)
{
oldestAge = c->GetAnimalAge();
oldestAnimal.clear();
oldestAnimal.push_back(anim);
}
}
if (Dog *d = dynamic_cast<Dog *>(anim))
{
if (d->GetAnimalAge() == oldestAge)
{
oldestAnimal.push_back(anim);
}
else if (d->GetAnimalAge() == youngestAge)
{
youngestAnimal.push_back(anim);
}
else if (d->GetAnimalAge() < youngestAge)
{
youngestAge = d->GetAnimalAge();
youngestAnimal.clear();
youngestAnimal.push_back(anim);
}
else if (d->GetAnimalAge() > oldestAge)
{
oldestAge = d->GetAnimalAge();
oldestAnimal.clear();
oldestAnimal.push_back(anim);
}
}
if (Fish *f = dynamic_cast<Fish *>(anim))
{
if (f->GetAnimalAge() == oldestAge)
{
oldestAnimal.push_back(anim);
}
else if (f->GetAnimalAge() == youngestAge)
{
youngestAnimal.push_back(anim);
}
else if (f->GetAnimalAge() < youngestAge)
{
youngestAge = f->GetAnimalAge();
youngestAnimal.clear();
youngestAnimal.push_back(anim);
}
else if (f->GetAnimalAge() > oldestAge)
{
oldestAge = f->GetAnimalAge();
oldestAnimal.clear();
oldestAnimal.push_back(anim);
}
}
}
}
std::cout << "Oldest animal(s):\n";
for (Animal *anm : oldestAnimal)
{
std::cout << *anm;
}
std::cout << "Youngest animal(s):\n";
for (Animal *anm : youngestAnimal)
{
std::cout << *anm;
}
std::cout<<"---------------------------------------------\nExample of method Play():\n";
owners[0].Play();
owners[3].Play();
owners[1].Play();
owners[2].Play();
for (Animal *anim : anims) // освобождение памяти
{
delete anim;
}
}