Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 14 / Farm
diff options
context:
space:
mode:
Commitf9cb26728021bf85a61f263fdb711c093a7f749a (patch)
Author
 angeld55 <angel_dimitriev@abv.bg>
2022-05-31 20:21:57 +0300
Committer
 angeld55 <angel_dimitriev@abv.bg>
2022-05-31 20:21:57 +0300
Files655fae8997b597c040d0dde06598a6414f64c21e /Sem. 14/Farm
Object-oriented_programming_FMI-f9cb26728021bf85a61f263fdb711c093a7f749a.tar
Object-oriented_programming_FMI-f9cb26728021bf85a61f263fdb711c093a7f749a.tar.gz
Object-oriented_programming_FMI-f9cb26728021bf85a61f263fdb711c093a7f749a.zip
Parente012bcac0a5c5fc804fbeadb0e8ab382c5e6feb7 (diff)
Sem 14
diff --git a/Sem. 14/Farm/Animals/Animal.h b/Sem. 14/Farm/Animals/Animal.h
new file mode 100644
index 0000000..8038843
--- /dev/null
+++ b/Sem. 14/Farm/Animals/Animal.h
@@ -0,0 +1,27 @@
+#pragma once
+#include <iostream>
+class Animal
+{
+
+public:
+
+ enum AnimalType
+ {
+ Dog = 0,
+ Cat = 1,
+ Mouse = 2
+ };
+ virtual void sayHello() const = 0;
+
+ virtual Animal* clone() const = 0;//създава копие на текущия обект.
+
+ virtual ~Animal() {} //!!!
+
+ AnimalType getType() const
+ {
+ return myType;
+ }
+
+protected:
+ AnimalType myType;
+}; \ No newline at end of file
diff --git a/Sem. 14/Farm/Animals/Cat.cpp b/Sem. 14/Farm/Animals/Cat.cpp
new file mode 100644
index 0000000..8716d4b
--- /dev/null
+++ b/Sem. 14/Farm/Animals/Cat.cpp
@@ -0,0 +1,12 @@
+#include "Cat.h"
+
+void Cat::sayHello() const
+{
+ std::cout << "Hello, I am a Cat!" << std::endl;
+}
+
+Animal* Cat::clone() const
+{
+ Animal* newObj = new Cat(*this); //copy constr of Mouse
+ return newObj;
+} \ No newline at end of file
diff --git a/Sem. 14/Farm/Animals/Cat.h b/Sem. 14/Farm/Animals/Cat.h
new file mode 100644
index 0000000..57aa627
--- /dev/null
+++ b/Sem. 14/Farm/Animals/Cat.h
@@ -0,0 +1,12 @@
+#pragma once
+#include "Animal.h"
+class Cat : public Animal
+{
+public:
+ Cat()
+ {
+ myType = Animal::AnimalType::Cat;
+ }
+ void sayHello() const override;
+ Animal* clone() const override;
+}; \ No newline at end of file
diff --git a/Sem. 14/Farm/Animals/Dog.cpp b/Sem. 14/Farm/Animals/Dog.cpp
new file mode 100644
index 0000000..879957f
--- /dev/null
+++ b/Sem. 14/Farm/Animals/Dog.cpp
@@ -0,0 +1,12 @@
+#include "Dog.h"
+
+void Dog::sayHello() const
+{
+ std::cout << "Hello, I am a Dog!" << std::endl;
+}
+
+Animal* Dog::clone() const
+{
+ Animal* newObj = new Dog(*this); //copy constr of Mouse
+ return newObj;
+} \ No newline at end of file
diff --git a/Sem. 14/Farm/Animals/Dog.h b/Sem. 14/Farm/Animals/Dog.h
new file mode 100644
index 0000000..3bf27b9
--- /dev/null
+++ b/Sem. 14/Farm/Animals/Dog.h
@@ -0,0 +1,12 @@
+#pragma once
+#include "Animal.h"
+class Dog : public Animal
+{
+public:
+ Dog()
+ {
+ myType = Animal::AnimalType::Dog;
+ }
+ void sayHello() const override;
+ Animal* clone() const override;
+}; \ No newline at end of file
diff --git a/Sem. 14/Farm/Animals/Mouse.cpp b/Sem. 14/Farm/Animals/Mouse.cpp
new file mode 100644
index 0000000..e3789ce
--- /dev/null
+++ b/Sem. 14/Farm/Animals/Mouse.cpp
@@ -0,0 +1,12 @@
+#include "Mouse.h"
+
+void Mouse::sayHello() const
+{
+ std::cout << "Hello, I am a Mouse!" << std::endl;
+}
+
+Animal* Mouse::clone() const
+{
+ Animal* newObj = new Mouse(*this); //copy constr of Mouse
+ return newObj;
+} \ No newline at end of file
diff --git a/Sem. 14/Farm/Animals/Mouse.h b/Sem. 14/Farm/Animals/Mouse.h
new file mode 100644
index 0000000..b979fa3
--- /dev/null
+++ b/Sem. 14/Farm/Animals/Mouse.h
@@ -0,0 +1,12 @@
+#pragma once
+#include "Animal.h"
+class Mouse : public Animal
+{
+public:
+ Mouse()
+ {
+ myType = Animal::AnimalType::Mouse;
+ }
+ void sayHello() const override;
+ Animal* clone() const override;
+}; \ No newline at end of file
diff --git a/Sem. 14/Farm/Collection/Farm.cpp b/Sem. 14/Farm/Collection/Farm.cpp
new file mode 100644
index 0000000..b604a2e
--- /dev/null
+++ b/Sem. 14/Farm/Collection/Farm.cpp
@@ -0,0 +1,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;
+} \ No newline at end of file
diff --git a/Sem. 14/Farm/Collection/Farm.h b/Sem. 14/Farm/Collection/Farm.h
new file mode 100644
index 0000000..7742973
--- /dev/null
+++ b/Sem. 14/Farm/Collection/Farm.h
@@ -0,0 +1,32 @@
+#pragma once
+#include "../Animals/Animal.h"
+#include "../Factory/Factory.h"
+
+class Farm
+{
+ Animal** animals;
+ size_t animalsCount;
+ size_t capacity;
+
+ void free();
+ void copyFrom(const Farm& other);
+ void resize();
+
+ AnimalFactory factory; //better singelton
+
+public:
+ Farm();
+ Farm(const Farm& other);
+ Farm& operator=(const Farm& other);
+ ~Farm();
+
+ void addAnimal(const Animal& animal);
+ bool addAnimal(Animal::AnimalType animalType);
+
+ void allSayHello() const;
+
+ size_t getCount() const;
+ bool isEmpty() const;
+
+ Animal::AnimalType getTypeByIndex(size_t index) const;
+};
diff --git a/Sem. 14/Farm/Factory/Factory.cpp b/Sem. 14/Farm/Factory/Factory.cpp
new file mode 100644
index 0000000..d7d6ec5
--- /dev/null
+++ b/Sem. 14/Farm/Factory/Factory.cpp
@@ -0,0 +1,13 @@
+#include "Factory.h"
+
+
+Animal* AnimalFactory::createAnimal(Animal::AnimalType type)
+{
+ switch (type)
+ {
+ case Animal::AnimalType::Cat: return new Cat();
+ case Animal::AnimalType::Dog: return new Dog();
+ case Animal::AnimalType::Mouse: return new Mouse();
+ }
+ return nullptr;
+}
diff --git a/Sem. 14/Farm/Factory/Factory.h b/Sem. 14/Farm/Factory/Factory.h
new file mode 100644
index 0000000..5532f4f
--- /dev/null
+++ b/Sem. 14/Farm/Factory/Factory.h
@@ -0,0 +1,10 @@
+#include "../Animals/Animal.h"
+#include "../Animals/Dog.h"
+#include "../Animals/Cat.h"
+#include "../Animals/Mouse.h"
+
+class AnimalFactory
+{
+public:
+ virtual Animal* createAnimal(Animal::AnimalType type);
+}; \ No newline at end of file
diff --git a/Sem. 14/Farm/source.cpp b/Sem. 14/Farm/source.cpp
new file mode 100644
index 0000000..e72e7ca
--- /dev/null
+++ b/Sem. 14/Farm/source.cpp
@@ -0,0 +1,19 @@
+#include <iostream>
+#include "../Farm/Collection/Farm.h"
+
+int main()
+{
+ Farm myFarm;
+
+ while (1)
+ {
+ size_t animalId;
+ std::cin >> animalId;
+ bool result = myFarm.addAnimal((Animal::AnimalType)animalId);
+
+ if (result)
+ std::cout << "Added animal of type: " << myFarm.getTypeByIndex(myFarm.getCount() - 1) << std::endl;
+ else
+ std::cout << "Invalid animal type" << std::endl;
+ }
+} \ No newline at end of file