blob: b604a2e696d943536100b6fbe8e99e3ed5b12a15 (
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
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
|
#include "Farm.h"
#include "../Animals/Dog.h"
void Farm::free()
{
for (size_t i = 0; i < animalsCount; i++)
delete animals[i]; //не се инт. какъв обект е. (вирт дестр)
delete[] animals;
}
void Farm::copyFrom(const Farm& other)
{
animals = new Animal * [other.capacity];
animalsCount = other.animalsCount;
capacity = other.capacity;
for (size_t i = 0; i < animalsCount; i++)
{
animals[i] = other.animals[i]->clone();
}
}
void Farm::resize()
{
Animal** newCollection = new Animal * [capacity *= 2];
for (size_t i = 0; i < animalsCount; i++)
newCollection[i] = animals[i]; // !!не правим клониране
delete[] animals;
animals = newCollection;
}
void Farm::addAnimal(const Animal& currentAnimal)
{
if (animalsCount == capacity)
resize();
animals[animalsCount++] = currentAnimal.clone();
}
Farm::Farm()
{
capacity = 8;
animalsCount = 0;
animals = new Animal * [capacity];
}
Farm::Farm(const Farm& other)
{
copyFrom(other);
}
Farm& Farm::operator=(const Farm& other)
{
if (this != &other)
{
free();
copyFrom(other);
}
return *this;
}
Farm::~Farm()
{
free();
}
void Farm::allSayHello() const
{
for (size_t i = 0; i < animalsCount; i++)
animals[i]->sayHello();
}
Animal::AnimalType Farm::getTypeByIndex(size_t index) const
{
if (index >= animalsCount)
throw std::out_of_range("No such index!");
return animals[index]->getType();
}
bool Farm::addAnimal(Animal::AnimalType animalType)
{
Animal* ptr = factory.createAnimal(animalType);
if (ptr != nullptr)
{
if (animalsCount == capacity)
resize();
animals[animalsCount++] = ptr;
}
}
size_t Farm::getCount() const
{
return animalsCount;
}
bool Farm::isEmpty() const
{
return animalsCount == 0;
}
|