Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit3dbf662b6d81659203e2273109a0b657c2f71d8b (patch)
Author
 Angel Dimitriev <achi_d@abv.bg>
2021-06-03 06:14:26 -0400
Committer
 GitHub <noreply@github.com>
2021-06-03 06:14:26 -0400
Files4542584cb422737cd46c541a3820ace6a9b6075b
Object-oriented_programming_FMI-3dbf662b6d81659203e2273109a0b657c2f71d8b.tar
Object-oriented_programming_FMI-3dbf662b6d81659203e2273109a0b657c2f71d8b.tar.gz
Object-oriented_programming_FMI-3dbf662b6d81659203e2273109a0b657c2f71d8b.zip
Parenteac106537581dd26df0a07649acd4111470f0adf (diff)
Update README.md
1 files changed, 152 insertions, 0 deletions
-rw-r--r--Sem. 14/README.md152
diff --git a/Sem. 14/README.md b/Sem. 14/README.md
index e69de29..5f4ec83 100644
--- a/Sem. 14/README.md
+++ b/Sem. 14/README.md
@@ -0,0 +1,152 @@
+
+# Колекции от обекти в полиморфна йерархия. Копиране и триене.
+## Колекция.
+
+Можем да реализираме колекция от различни типове (но с общ базов клас), чрез масив от указатели. Указателите трябва да са от типа на базовия клас.
+
+```c++
+struct Fruit
+{
+ virtual void printColor() const = 0;
+
+ virtual ~Fruit(){}
+}
+```c++
+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++
+```c++
+struct Fruit
+{
+ virtual void printColor() const = 0;
+ virtual ~Fruit(){}
+
+ virtual Fruit* clone() const = 0; //!
+}
+```c++
+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;
+}
+ ```
+