Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 13 / Farm / Farm.cpp
diff options
context:
space:
mode:
Commitc1a310e69933035fb385f52275e9b80cdad695fc (patch)
Author
 Angel Dimitriev <achi_d@abv.bg>
2022-05-24 19:38:22 +0300
Committer
 GitHub <noreply@github.com>
2022-05-24 19:38:22 +0300
Files65bf3496cdae1f59e68cc741de5d2b9dfba648d0 /Sem. 13/Farm/Farm.cpp
Object-oriented_programming_FMI-c1a310e69933035fb385f52275e9b80cdad695fc.tar
Object-oriented_programming_FMI-c1a310e69933035fb385f52275e9b80cdad695fc.tar.gz
Object-oriented_programming_FMI-c1a310e69933035fb385f52275e9b80cdad695fc.zip
Parentc7daf590047fd6f2039fed4f67a643a2178988d1 (diff)
Add files via upload
1 files changed, 63 insertions, 0 deletions
-rw-r--r--Sem. 13/Farm/Farm.cpp63
diff --git a/Sem. 13/Farm/Farm.cpp b/Sem. 13/Farm/Farm.cpp
new file mode 100644
index 0000000..1328e95
--- /dev/null
+++ b/Sem. 13/Farm/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.animalsCount];
+ 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();
+}