Website Summary Files Commits Branches @commit @diff @stats
path: root / Polymorphism / Farm(cloneFunction)
diff options
context:
space:
mode:
Commit0218c678e13e1cb94a352c91733cea68467dc566 (patch)
Author
 Angeld55 <angel_dimitriev@abv.bg>
2021-02-14 23:06:35 +0200
Committer
 Angeld55 <angel_dimitriev@abv.bg>
2021-02-14 23:06:35 +0200
Files16fb3114cb560242458d10411765e50f5d6183db /Polymorphism/Farm(cloneFunction)
Object-oriented_programming_FMI-0218c678e13e1cb94a352c91733cea68467dc566.tar
Object-oriented_programming_FMI-0218c678e13e1cb94a352c91733cea68467dc566.tar.gz
Object-oriented_programming_FMI-0218c678e13e1cb94a352c91733cea68467dc566.zip
Parentd4ae0ef9ec1b54420176c4a88e09f2fd44ae31b0 (diff)
start from scratch
diff --git a/Polymorphism/Farm(cloneFunction)/Animal.hpp b/Polymorphism/Farm(cloneFunction)/Animal.hpp
deleted file mode 100644
index 0f290ca..0000000
--- a/Polymorphism/Farm(cloneFunction)/Animal.hpp
+++ /dev/null
@@ -1,19 +0,0 @@
-#pragma once
-#include <iostream>
-#include <string>
-using namespace std;
-class Animal
-{
-
- string name;
-public:
- Animal(string name)
- {
- this->name = name;
- }
- virtual void Roar() = 0;
- virtual Animal* clone() = 0; // Всеки наследник се копира по различен начин.
- // За това правим тази виртуална функция и всеки наследник да окаже как се копират данните му.
- virtual ~Animal() {};
-
-};
diff --git a/Polymorphism/Farm(cloneFunction)/Cat.hpp b/Polymorphism/Farm(cloneFunction)/Cat.hpp
deleted file mode 100644
index eba7ff0..0000000
--- a/Polymorphism/Farm(cloneFunction)/Cat.hpp
+++ /dev/null
@@ -1,18 +0,0 @@
-#include "Animal.hpp"
-#include <iostream>
-
-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;
- }
-};
diff --git a/Polymorphism/Farm(cloneFunction)/Cow.hpp b/Polymorphism/Farm(cloneFunction)/Cow.hpp
deleted file mode 100644
index ba576a2..0000000
--- a/Polymorphism/Farm(cloneFunction)/Cow.hpp
+++ /dev/null
@@ -1,16 +0,0 @@
-#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;
- }
-};
diff --git a/Polymorphism/Farm(cloneFunction)/Dog.hpp b/Polymorphism/Farm(cloneFunction)/Dog.hpp
deleted file mode 100644
index 9928c2a..0000000
--- a/Polymorphism/Farm(cloneFunction)/Dog.hpp
+++ /dev/null
@@ -1,18 +0,0 @@
-#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;
- }
-
-};
diff --git a/Polymorphism/Farm(cloneFunction)/Farm.hpp b/Polymorphism/Farm(cloneFunction)/Farm.hpp
deleted file mode 100644
index 1ad3be5..0000000
--- a/Polymorphism/Farm(cloneFunction)/Farm.hpp
+++ /dev/null
@@ -1,106 +0,0 @@
-#include "Animal.hpp"
-#include "Cat.hpp"
-#include "Dog.hpp"
-#include "Cow.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++]=an;
- 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;
-}
-
diff --git a/Polymorphism/Farm(cloneFunction)/Source.cpp b/Polymorphism/Farm(cloneFunction)/Source.cpp
deleted file mode 100644
index d6fcfc9..0000000
--- a/Polymorphism/Farm(cloneFunction)/Source.cpp
+++ /dev/null
@@ -1,13 +0,0 @@
-#include "Farm.hpp"
-
-using namespace std;
-int main()
-{
- Farm f(4);
- f.AddCat("Cat1");
- f.AddCat("Cat2");
- f.AddCow("Cow1");
- f.AddDog("Dog1");
-
- f.RoarAll();
-}