From 769773c5fd9318348eb31d17b219f00fca265eb3 Mon Sep 17 00:00:00 2001 From: Angeld55 Date: Fri, 20 Sep 2019 14:29:01 +0300 Subject: Add files via upload --- .../Figures/Farm(cloneFunction)/Animal.hpp | 18 +++- Polymorphism/Figures/Farm(cloneFunction)/Cat.hpp | 17 ++++ Polymorphism/Figures/Farm(cloneFunction)/Cow.hpp | 16 ++++ Polymorphism/Figures/Farm(cloneFunction)/Dog.hpp | 18 ++++ Polymorphism/Figures/Farm(cloneFunction)/Farm.hpp | 98 ++++++++++++++++++++++ 5 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 Polymorphism/Figures/Farm(cloneFunction)/Cat.hpp create mode 100644 Polymorphism/Figures/Farm(cloneFunction)/Cow.hpp create mode 100644 Polymorphism/Figures/Farm(cloneFunction)/Dog.hpp create mode 100644 Polymorphism/Figures/Farm(cloneFunction)/Farm.hpp (limited to 'Polymorphism') diff --git a/Polymorphism/Figures/Farm(cloneFunction)/Animal.hpp b/Polymorphism/Figures/Farm(cloneFunction)/Animal.hpp index b478595..dc9cedd 100644 --- a/Polymorphism/Figures/Farm(cloneFunction)/Animal.hpp +++ b/Polymorphism/Figures/Farm(cloneFunction)/Animal.hpp @@ -1 +1,17 @@ -s +#include "Animal.hpp" + +class Animal +{ + + String name; +public: + Animal(String name) + { + this->name = name; + } + virtual void Roar() = 0; + virtual Animal* clone() = 0; // Всеки наследник се копира по различен начин. + // За това правим тази виртуална функция и всеки наследник да окаже как се копират данните му. + virtual ~Animal() {}; + +}; \ No newline at end of file diff --git a/Polymorphism/Figures/Farm(cloneFunction)/Cat.hpp b/Polymorphism/Figures/Farm(cloneFunction)/Cat.hpp new file mode 100644 index 0000000..da76fe3 --- /dev/null +++ b/Polymorphism/Figures/Farm(cloneFunction)/Cat.hpp @@ -0,0 +1,17 @@ +#include "Animal.hpp" + +class Cat : public Animal +{ +public: + Cat(String name) :Animal(name) + {} + Animal* clone() + { + Animal* copy = new Cat(*this); + return copy; + } + void Roar() + { + std::cout << "meow meow" << std::endl; + } +}; \ No newline at end of file diff --git a/Polymorphism/Figures/Farm(cloneFunction)/Cow.hpp b/Polymorphism/Figures/Farm(cloneFunction)/Cow.hpp new file mode 100644 index 0000000..58ad7e7 --- /dev/null +++ b/Polymorphism/Figures/Farm(cloneFunction)/Cow.hpp @@ -0,0 +1,16 @@ +#include "Animal.hpp" +class Cow : public Animal +{ +public: + Cow(String name) :Animal(name) + {} + Animal* clone() + { + Animal* copy = new Cow(*this); + return copy; + } + void Roar() + { + std::cout << "muu mmuu" << std::endl; + } +}; \ No newline at end of file diff --git a/Polymorphism/Figures/Farm(cloneFunction)/Dog.hpp b/Polymorphism/Figures/Farm(cloneFunction)/Dog.hpp new file mode 100644 index 0000000..8d26b63 --- /dev/null +++ b/Polymorphism/Figures/Farm(cloneFunction)/Dog.hpp @@ -0,0 +1,18 @@ +#include "Animal.hpp" + +class Dog : public Animal +{ +public: + Dog(String name) :Animal(name) + {} + void Roar() + { + std::cout << "bau bau" << std::endl; + } + Animal* clone() + { + Animal* copy = new Dog(*this); + return copy; + } + +}; \ No newline at end of file diff --git a/Polymorphism/Figures/Farm(cloneFunction)/Farm.hpp b/Polymorphism/Figures/Farm(cloneFunction)/Farm.hpp new file mode 100644 index 0000000..51a980e --- /dev/null +++ b/Polymorphism/Figures/Farm(cloneFunction)/Farm.hpp @@ -0,0 +1,98 @@ +#include "Animal.hpp" + +// Collection of all animals +class Farm +{ + Animal** animals; + int len; + int count; + void Free(); + void CopyFrom(const Farm& other); + bool AddAnimal(Animal* an); +public: + Farm(int len); + Farm(const Farm& other); + Farm& operator=(const Farm& other); + ~Farm(); + bool AddCat(std::string name); + bool AddDog(std::string name); + bool AddCow(std::string name); + void RoarAll(); +}; +Farm::Farm(int len) +{ + animals = new Animal*[len]; + this->len = len; + count = 0; +} +Farm::Farm(const Farm& other) +{ + CopyFrom(other); +} +Farm& Farm::operator=(const Farm& other) +{ + if (this != &other) + { + Free(); + CopyFrom(other); + } + return *this; +} +Farm::~Farm() +{ + Free(); +} +bool Farm::AddAnimal(Animal* an) +{ + if(count == len) + return false; + animals[count++]=c; + return true; +} +bool Farm::AddCat(std::string name) +{ + Cat* c = new Cat(name); + if(!AddAnimal(c)) + { + delete c; + return false; + } +} +bool Farm::AddDog(std::string name) +{ + Dog* c = new Dog(name); + if(!AddAnimal(c)) + { + delete c; + return false; + } +} +bool Farm::AddCow(std::string name) +{ + Cow* c = new Cow(name); + if(!AddAnimal(c)) + { + delete c; + return false; + } +} +void Farm::RoarAll() +{ + for (int i = 0; i < count; i++) + animals[i]->Roar(); //roar is a virtual function + +} +void Farm::Free() +{ + for (int i = 0; i < count; i++) + delete animals[i]; //~Animal() is virtual + delete[] animals; +} +void Farm::CopyFrom(const Farm& other) +{ + animals = new Animal*[other.len]; + for (int i = 0; i < other.count; i++) + animals[i] = other.animals[i]->clone(); //clone is a virtual function + len = other.len; + count = other.count; +} \ No newline at end of file -- cgit v1.3.1-23-gd82d