blob: acf638228bac8b6f5254524a03fd8a9acb08b3a3 (
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
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
|
# Колекции от обекти в полиморфна йерархия. Копиране и триене.
## Колекция.
Можем да реализираме колекция от различни типове (но с общ базов клас), чрез масив от указатели. Указателите трябва да са от типа на базовия клас.
```c++
struct Fruit
{
virtual void printColor() const = 0;
virtual ~Fruit(){}
};
struct Apple
{
void printColor() const override
{
std::cout << "Red or green! "<<std::endl;
}
};
struct Orange
{
void printColor() const override
{
std::cout << "Orange! "<<std::endl;
}
};
struct Lemon
{
Lemon(size_t sourness) : sourness(sourness){}
size_t sourness;
void printColor() const override
{
std::cout << "Yellow! "<<std::endl;
}
};
class FruitStore
{
private:
Fruit** fruits;
size_t capacity;
size_t count;
void copyFrom(const FruitStore& other);
void free();
public:
FruitStore();
FruitStore(const FruitStore&);
FruitStore& operator=(const FruitStore&);
~FruitStore();
void addApple();
void addOrange();
void addLemon(size_t sourness);
};
void FruitStore::addApple()
{
fruits[count++] = new Apple();
}
void FruitStore::addOrange()
{
fruits[count++] = new Orange();
}
void FruitStore::addLemon(size_t sourness)
{
fruits[count++] = new Lemon(sourness);
}
```
## Триене
Понеже имаме виртуален деструктор в базовия клас, не се интересуваме в колекцията какви са обектите, които трием.
```c++
void FruitStore::free()
{
for(int i = 0; i < count; i++)
delete fruits[i];
delete[] fruits;
}
```
## Копиране
Искаме да реализираме копиране на колекцията.
Това трябва да стане без да нарушаваме абстракцията - искаме обеките да се копират без да се налага да запитваме за техния тип.
За това дефинираме виртуална функция clone, която ще връща копие на обекта. Тази функция я разписваме във всеки от наследниците.
```c++
struct Fruit
{
virtual void printColor() const = 0;
virtual ~Fruit(){}
virtual Fruit* clone() const = 0; //!
};
struct Apple
{
void printColor() const override
{
std::cout << "Red or green! "<<std::endl;
}
Fruit* clone() const override
{
return new Apple(*this);
}
};
struct Orange
{
void printColor() const override
{
std::cout << "Orange! "<<std::endl;
}
Fruit* clone() const override
{
return new Apple(*this);
}
};
struct Lemon
{
Lemon(size_t sourness) : sourness(sourness){}
size_t sourness;
void printColor() const override
{
std::cout << "Yellow! "<<std::endl;
}
Fruit* clone() const override
{
return new Lemon(*this);
}
};
```
От тук копирането става тривиално:
```c++
void FruitStore::copyFrom(const FruitStore& other)
{
fruits = new Fruit*[other. capacity];
for(int i = 0; i < other.count; i++)
fruits[i] = other.fruits[u]->clone();
count = other.count;
capacity = other.capacity;
}
```
|