Website Summary Files Commits Branches @commit @diff @stats
path: root / Polymorphism
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
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();
-}
diff --git a/Polymorphism/MathExpression/Expression.hpp b/Polymorphism/MathExpression/Expression.hpp
deleted file mode 100644
index 4f09790..0000000
--- a/Polymorphism/MathExpression/Expression.hpp
+++ /dev/null
@@ -1,16 +0,0 @@
-#pragma once
-
-#include <iostream>
-
-class Expression
-{
-public:
- virtual void Print() = 0;
- virtual int Evaluate() = 0;
- virtual ~Expression(){}
- int getType(){ return type; }
-protected:
- int type; //Ако обектът е NUM, ще пазим тук 1, а ако обектът е Операция, ще пазим тук 2.
-};
-
-
diff --git a/Polymorphism/MathExpression/Num.hpp b/Polymorphism/MathExpression/Num.hpp
deleted file mode 100644
index 91e1492..0000000
--- a/Polymorphism/MathExpression/Num.hpp
+++ /dev/null
@@ -1,26 +0,0 @@
-#pragma once
-
-#include "Expression.hpp"
-class Num : public Expression
-{
- int number;
-public:
- Num(int number)
- {
- this->number = number;
- type = 1;
- }
- void Print()
- {
- std::cout << number;
- }
- int Evaluate()
- {
- return number;
- }
- int getNum()
- {
- return number;
- }
-
-}; \ No newline at end of file
diff --git a/Polymorphism/MathExpression/Operation.hpp b/Polymorphism/MathExpression/Operation.hpp
deleted file mode 100644
index ade8404..0000000
--- a/Polymorphism/MathExpression/Operation.hpp
+++ /dev/null
@@ -1,48 +0,0 @@
-#pragma once
-#include "Expression.hpp"
-class Operation : public Expression
-{
-
-public:
- Expression* left;
- char op;
- Expression* right;
- Operation(Expression* left, char ch, Expression* right)
- {
-
- this->left = left;
- op = ch;
- this->right = right;
- type = 2;
-
- }
- void Print()
- {
- std::cout << '(';
- left->Print();
- std::cout << op;
- right->Print();
- std::cout << ')';
- }
- int Evaluate()
- {
- int result = left->Evaluate();
- int value2 = right->Evaluate();
-
- if (op == '+')
- result += value2;
- else if (op == '*')
- result *= value2;
- else
- return 0; //Invalid operation
- return result;
- }
- ~Operation()
- {
-
- delete left;
- delete right;
- op = '\0';
- }
-
-}; \ No newline at end of file
diff --git a/Polymorphism/MathExpression/README.md b/Polymorphism/MathExpression/README.md
deleted file mode 100644
index 4472fb4..0000000
--- a/Polymorphism/MathExpression/README.md
+++ /dev/null
@@ -1,8 +0,0 @@
-# Hierarchy
-
-
-![enter image description here](https://i.ibb.co/ZHnWnKG/1-hierarchy.jpg "Hierarchy")
-
-# How does an object looks like
-
-![enter image description here](https://i.ibb.co/KFfsf7S/2-Example-Expr.jpg "Example")
diff --git a/Polymorphism/MathExpression/main.hpp b/Polymorphism/MathExpression/main.hpp
deleted file mode 100644
index 5fa3f8f..0000000
--- a/Polymorphism/MathExpression/main.hpp
+++ /dev/null
@@ -1,43 +0,0 @@
-#include "Expression.hpp"
-#include "Num.hpp"
-#include "Operation.hpp"
-bool AreTheSame(Expression* left, Expression* right);
-int main()
-{
- Expression* l = new Num(3);
- Expression* r = new Num(7);
- Expression* final1 = new Operation(l, '+', r);
-
- Expression* l2 = new Num(3);
- Expression* r2 = new Num(7);
- Expression* final2 = new Operation(l2, '+', r2);
-
- std::cout << final1->Evaluate();
-
- std::cout << AreTheSame(final1, final2);
-
- delete final1, final2; //Винаги трием най-големите 'Expression-и'. Те се грижат за по-малките от които са образувани.
-
-
-
-}
-bool AreTheSame(Expression* left, Expression* right)
-{
- if (left->getType() != right->getType())//гледаме дали не са от раличен тип (едното да е опрация,другото да е число)
- return false; //очевидно са различни тогава
-
- if (left->getType() == 1) //ако двете са числа
- {
- Num* l = (Num*)left; //Конвертираме пойнтерите
- Num* r = (Num*)right;
- return l->getNum() == r->getNum(); //сравняваме стойностите
- }
- else //Ако двете са операции
- {
- Operation* l = (Operation*)left;
- Operation* r = (Operation*)right;
- return (l->op == l->op) && AreTheSame(l->left, r->left) && AreTheSame(l->right, r->right);
- //гледаме дали знаците на опрациите съвпадат, дали левите страни съвпадат и дали десните страни съвпадат.
- }
-
-}
diff --git a/Polymorphism/Shapes/Circle.cpp b/Polymorphism/Shapes/Circle.cpp
deleted file mode 100644
index e625123..0000000
--- a/Polymorphism/Shapes/Circle.cpp
+++ /dev/null
@@ -1,19 +0,0 @@
-#include "Circle.h"
-
-Circle::Circle(int x, int y, int r) : Shape(1), radius(r)
-{
- setPoint(x, y, 0);
-}
-double Circle::getArea()
-{
- return PI * radius * radius;
-}
-double Circle::getPer()
-{
- return 2 * PI * radius;
-}
-Shape* Circle::clone()
-{
- Circle* newCircle = new Circle(*this);
- return newCircle;
-} \ No newline at end of file
diff --git a/Polymorphism/Shapes/Circle.h b/Polymorphism/Shapes/Circle.h
deleted file mode 100644
index c648690..0000000
--- a/Polymorphism/Shapes/Circle.h
+++ /dev/null
@@ -1,16 +0,0 @@
-#include "Shape.h"
-
-const double PI = 3.14;
-class Circle : public Shape
-{
-
- int radius;
-public:
- Circle(int x, int y, int radius);
-
- double getArea();
- double getPer();
-
- Shape* clone();
-
-}; \ No newline at end of file
diff --git a/Polymorphism/Shapes/Point.h b/Polymorphism/Shapes/Point.h
deleted file mode 100644
index acacb85..0000000
--- a/Polymorphism/Shapes/Point.h
+++ /dev/null
@@ -1,18 +0,0 @@
-#pragma once
-#include <iostream>
-struct Point
-{
- int x;
- int y;
- Point(int x, int y)
- {
- this->x = x;
- this->y = y;
- }
- Point() : Point(0,0)
- {}
- double getDist(const Point& other) const
- {
- return sqrt((x - other.x)*(x - other.x) + (y - other.y) * (y - other.y));
- }
-}; \ No newline at end of file
diff --git a/Polymorphism/Shapes/Rectangle.cpp b/Polymorphism/Shapes/Rectangle.cpp
deleted file mode 100644
index b684c50..0000000
--- a/Polymorphism/Shapes/Rectangle.cpp
+++ /dev/null
@@ -1,24 +0,0 @@
-#include "Rectangle.h"
-
-
-Rectangle::Rectangle(int x1, int y1, int x3, int y3) : Shape(4)
-{
- setPoint(x1, y1, 0);
- setPoint(x3, y3, 2);
- setPoint(x1, y3, 1);
- setPoint(x3, y1, 3);
-}
-
-double Rectangle::getPer()
-{
- return 2 * (getPoint(0).getDist(getPoint(1)) + getPoint(1).getDist(getPoint(2)) );
-}
-double Rectangle::getArea()
-{
- return getPoint(0).getDist(getPoint(1)) * getPoint(1).getDist(getPoint(2));
-}
-Shape* Rectangle::clone()
-{
- Rectangle* newRect = new Rectangle(*this);
- return newRect;
-} \ No newline at end of file
diff --git a/Polymorphism/Shapes/Rectangle.h b/Polymorphism/Shapes/Rectangle.h
deleted file mode 100644
index 681a021..0000000
--- a/Polymorphism/Shapes/Rectangle.h
+++ /dev/null
@@ -1,11 +0,0 @@
-#include "Shape.h"
-class Rectangle : public Shape
-{
-public:
- Rectangle(int x1, int y1, int x3, int y3);
-
- double getPer();
- double getArea();
-
- Shape* clone();
-}; \ No newline at end of file
diff --git a/Polymorphism/Shapes/Shape.cpp b/Polymorphism/Shapes/Shape.cpp
deleted file mode 100644
index 0751108..0000000
--- a/Polymorphism/Shapes/Shape.cpp
+++ /dev/null
@@ -1,50 +0,0 @@
-#include "Shape.h"
-
-Shape::Shape(int pointsCount)
-{
- points = new Point[pointsCount];
- this->pointsCount = pointsCount;
-}
-
-void Shape::setPoint(int x, int y, int ind)
-{
- Point current(x, y);
- points[ind] = current;
-
-}
-
-const Point& Shape::getPoint(int ind)
-{
- return points[ind];
-}
-
-void Shape::CopyFrom(const Shape& other)
-{
- points = new Point[other.pointsCount];
- pointsCount = other.pointsCount;
-
- for (int i = 0; i < pointsCount; i++)
- points[i] = other.points[i];
-}
-void Shape::Free()
-{
- delete[] points;
- pointsCount = 0;
-}
-Shape::Shape(const Shape& other)
-{
- CopyFrom(other);
-}
-Shape& Shape::operator=(const Shape& other)
-{
- if (this != &other)
- {
- Free();
- CopyFrom(other);
- }
- return *this;
-}
-Shape::~Shape()
-{
- Free();
-}
diff --git a/Polymorphism/Shapes/Shape.h b/Polymorphism/Shapes/Shape.h
deleted file mode 100644
index f31e42e..0000000
--- a/Polymorphism/Shapes/Shape.h
+++ /dev/null
@@ -1,27 +0,0 @@
-#pragma once
-
-#include "Point.h"
-
-class Shape
-{
- Point* points;
- int pointsCount;
-
- void CopyFrom(const Shape& other);
- void Free();
-
-public:
- Shape(int pointsCount);
- Shape(const Shape& other);
- Shape& operator=(const Shape& other);
- virtual ~Shape();
-
- void setPoint(int x, int y, int ind);
- const Point& getPoint(int ind);
-
- virtual double getPer() =0;
- virtual double getArea() = 0;
-
- virtual Shape* clone() = 0;
-
-}; \ No newline at end of file
diff --git a/Polymorphism/Shapes/ShapeCollection.cpp b/Polymorphism/Shapes/ShapeCollection.cpp
deleted file mode 100644
index 8fcad3c..0000000
--- a/Polymorphism/Shapes/ShapeCollection.cpp
+++ /dev/null
@@ -1,88 +0,0 @@
-#include "ShapeCollection.h"
-
-ShapeCollection::ShapeCollection()
-{
- count = 0;
- capacity = 8;
-
- data = new Shape*[capacity];
-}
-
-void ShapeCollection::AddCircle(int x1, int y1, int r)
-{
- Circle* current = new Circle(x1, y1, r);
- Add(current);
-
-}
-void ShapeCollection::AddTriagnle(int x1, int y1, int x2, int y2, int x3, int y3)
-{
- Triangle* current = new Triangle(x1, y1, x2, y2, x3, y3);
- Add(current);
-}
-void ShapeCollection::AddRect(int x1, int y1, int x2, int y2)
-{
- Rectangle* current = new Rectangle(x1, y1, x2, y2);
- Add(current);
-}
-void ShapeCollection::Add(Shape* currentShape)
-{
- if (count == capacity)
- Resize();
- data[count++] = currentShape;
-}
-void ShapeCollection::Free()
-{
- for (int i = 0; i < count; i++)
- delete data[i];
-
- delete[] data;
-}
-void ShapeCollection::CopyFrom(const ShapeCollection& other)
-{
- data = new Shape*[other.count];
- capacity = other.capacity;
- count = other.count;
- for (int i = 0; i < other.count; i++)
- data[i] = other.data[i]->clone();
-
-}
-ShapeCollection::ShapeCollection(const ShapeCollection& other)
-{
- CopyFrom(other);
-}
-ShapeCollection& ShapeCollection::operator = (const ShapeCollection& other)
-{
- if (this != &other)
- {
- Free();
- CopyFrom(other);
- }
- return *this;
-}
-ShapeCollection::~ShapeCollection()
-{
- Free();
-}
-void ShapeCollection::Resize()
-{
- Shape** temp = new Shape*[capacity * 2];
- capacity *= 2;
- for (int i = 0; i < count; i++)
- temp[i] = data[i];
- delete[] data;
- data = temp;
-
-
-}
-void ShapeCollection::PrintAllAreas()
-{
- for (int i = 0; i < count; i++)
- std::cout << data[i]->getArea() << " ";
- std::cout<<std::endl;
-}
-void ShapeCollection::PrintAllPers()
-{
- for (int i = 0; i < count; i++)
- std::cout << data[i]->getPer() << " ";
- std::cout << std::endl;
-} \ No newline at end of file
diff --git a/Polymorphism/Shapes/ShapeCollection.h b/Polymorphism/Shapes/ShapeCollection.h
deleted file mode 100644
index 36f6092..0000000
--- a/Polymorphism/Shapes/ShapeCollection.h
+++ /dev/null
@@ -1,30 +0,0 @@
-#include "Circle.h"
-#include "Rectangle.h"
-#include "Triangle.h"
-
-class ShapeCollection
-{
- Shape** data;
- int count;
- int capacity;
-
- void Resize();
-
- void Add(Shape* currentShape);
-
- void CopyFrom(const ShapeCollection& other);
- void Free();
-public:
- ShapeCollection();
-
- ShapeCollection(const ShapeCollection& other);
- ShapeCollection& operator=(const ShapeCollection& other);
- ~ShapeCollection();
-
- void AddCircle(int x1,int x2,int r);
- void AddTriagnle(int x1,int y1,int x2,int y2,int x3,int y3);
- void AddRect(int x1, int y1, int x2, int y2);
-
- void PrintAllAreas();
- void PrintAllPers();
-}; \ No newline at end of file
diff --git a/Polymorphism/Shapes/Source.cpp b/Polymorphism/Shapes/Source.cpp
deleted file mode 100644
index 3381111..0000000
--- a/Polymorphism/Shapes/Source.cpp
+++ /dev/null
@@ -1,30 +0,0 @@
-
-using namespace std;
-#include <fstream>
-#include <vector>
-#include "ShapeCollection.h"
-
-int main()
-{
- ShapeCollection sh;
-
- sh.AddCircle(3, 3, 5);
- sh.AddTriagnle(1, 4, 2, 11, 7, 4);
- sh.AddRect(1, 1, 4, 4);
-
- sh.PrintAllAreas();
-
-/* lamda fucntion example
- std::vector<Circle> s;
- s.push_back(Circle(3, 4, 4));
- s.push_back(Circle(1, 2, 3));
-
- std::sort(s.begin(), s.end(),
- [](Circle& lhs, Circle& rhs) -> bool
- {
- return lhs.getArea() < rhs.getArea();
- });*/
-
-
-
-} \ No newline at end of file
diff --git a/Polymorphism/Shapes/Triangle.cpp b/Polymorphism/Shapes/Triangle.cpp
deleted file mode 100644
index 545e2a6..0000000
--- a/Polymorphism/Shapes/Triangle.cpp
+++ /dev/null
@@ -1,31 +0,0 @@
-#include "Triangle.h"
-
-Triangle::Triangle(int x1, int y1, int x2, int y2, int x3, int y3) : Shape(3)
-{
- setPoint(x1, y1, 0);
- setPoint(x2, y2, 1);
- setPoint(x3, y3, 2);
-}
-
-double Triangle::getArea()
-{
- double sideA = getPoint(0).getDist(getPoint(1));
- double sideB = getPoint(1).getDist(getPoint(2));
- double sideC = getPoint(2).getDist(getPoint(0));
-
- double halfPer = getPer() / 2;
-
- return sqrt(halfPer * (halfPer - sideA) *
- (halfPer - sideB) * (halfPer - sideC));
-}
-double Triangle::getPer()
-{
- return getPoint(0).getDist(getPoint(1)) +
- getPoint(1).getDist(getPoint(2)) +
- getPoint(2).getDist(getPoint(0));
-}
-Shape* Triangle::clone()
-{
- Triangle* newTriangle = new Triangle(*this);
- return newTriangle;
-} \ No newline at end of file
diff --git a/Polymorphism/Shapes/Triangle.h b/Polymorphism/Shapes/Triangle.h
deleted file mode 100644
index b038e80..0000000
--- a/Polymorphism/Shapes/Triangle.h
+++ /dev/null
@@ -1,12 +0,0 @@
-#include "Shape.h"
-
-class Triangle : public Shape
-{
-public:
- Triangle(int x1, int y1, int x2, int y2, int x3, int y3);
-
- double getArea();
- double getPer();
-
- Shape* clone();
-}; \ No newline at end of file