From e012bcac0a5c5fc804fbeadb0e8ab382c5e6feb7 Mon Sep 17 00:00:00 2001 From: angeld55 Date: Tue, 31 May 2022 00:56:24 +0300 Subject: split in to folders --- Sem. 13/Farm/Collection/Farm.cpp | 63 ++++++++++++++++++++++++++++++++++++++++ Sem. 13/Farm/Collection/Farm.h | 24 +++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 Sem. 13/Farm/Collection/Farm.cpp create mode 100644 Sem. 13/Farm/Collection/Farm.h (limited to 'Sem. 13/Farm/Collection') diff --git a/Sem. 13/Farm/Collection/Farm.cpp b/Sem. 13/Farm/Collection/Farm.cpp new file mode 100644 index 0000000..626c543 --- /dev/null +++ b/Sem. 13/Farm/Collection/Farm.cpp @@ -0,0 +1,63 @@ +#include "Farm.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(); +} diff --git a/Sem. 13/Farm/Collection/Farm.h b/Sem. 13/Farm/Collection/Farm.h new file mode 100644 index 0000000..57b1494 --- /dev/null +++ b/Sem. 13/Farm/Collection/Farm.h @@ -0,0 +1,24 @@ +#pragma once +#include "../Animals/Animal.h" + +class Farm +{ + Animal** animals; + size_t animalsCount; + size_t capacity; + + void free(); + void copyFrom(const Farm& other); + void resize(); + +public: + Farm(); + Farm(const Farm& other); + Farm& operator=(const Farm& other); + ~Farm(); + + void addAnimal(const Animal& animal); + + void allSayHello() const; + +}; -- cgit v1.3.1-23-gd82d