Website Summary Files Commits Branches @commit @diff @stats
path: root / Polymorphism
diff options
context:
space:
mode:
Commit495fe9ae299e81c213161d992d05cc11a403a1d7 (patch)
Author
 Angel Dimitriev <achi_d@abv.bg>
2020-05-20 03:02:00 +0300
Committer
 GitHub <noreply@github.com>
2020-05-20 03:02:00 +0300
Files8d71f8728ddfcf86d34ec1690b144067a3e43887 /Polymorphism
Object-oriented_programming_FMI-495fe9ae299e81c213161d992d05cc11a403a1d7.tar
Object-oriented_programming_FMI-495fe9ae299e81c213161d992d05cc11a403a1d7.tar.gz
Object-oriented_programming_FMI-495fe9ae299e81c213161d992d05cc11a403a1d7.zip
Parentb7a69d6934a940bcdd8a61df3e6d1df439dc1b0a (diff)
Add files via upload
12 files changed, 356 insertions, 0 deletions
-rw-r--r--Polymorphism/Circle.cpp19
-rw-r--r--Polymorphism/Circle.h16
-rw-r--r--Polymorphism/Point.h18
-rw-r--r--Polymorphism/Rectangle.cpp24
-rw-r--r--Polymorphism/Rectangle.h11
-rw-r--r--Polymorphism/Shape.cpp50
-rw-r--r--Polymorphism/Shape.h27
-rw-r--r--Polymorphism/ShapeCollection.cpp88
-rw-r--r--Polymorphism/ShapeCollection.h30
-rw-r--r--Polymorphism/Source.cpp30
-rw-r--r--Polymorphism/Triangle.cpp31
-rw-r--r--Polymorphism/Triangle.h12
diff --git a/Polymorphism/Circle.cpp b/Polymorphism/Circle.cpp
new file mode 100644
index 0000000..6ba083f
--- /dev/null
+++ b/Polymorphism/Circle.cpp
@@ -0,0 +1,19 @@
+#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/Circle.h b/Polymorphism/Circle.h
new file mode 100644
index 0000000..ce355bf
--- /dev/null
+++ b/Polymorphism/Circle.h
@@ -0,0 +1,16 @@
+#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/Point.h b/Polymorphism/Point.h
new file mode 100644
index 0000000..f00b37d
--- /dev/null
+++ b/Polymorphism/Point.h
@@ -0,0 +1,18 @@
+#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/Rectangle.cpp b/Polymorphism/Rectangle.cpp
new file mode 100644
index 0000000..8f9d1fb
--- /dev/null
+++ b/Polymorphism/Rectangle.cpp
@@ -0,0 +1,24 @@
+#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/Rectangle.h b/Polymorphism/Rectangle.h
new file mode 100644
index 0000000..7c7e3d0
--- /dev/null
+++ b/Polymorphism/Rectangle.h
@@ -0,0 +1,11 @@
+#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/Shape.cpp b/Polymorphism/Shape.cpp
new file mode 100644
index 0000000..787ec82
--- /dev/null
+++ b/Polymorphism/Shape.cpp
@@ -0,0 +1,50 @@
+#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/Shape.h b/Polymorphism/Shape.h
new file mode 100644
index 0000000..fbce027
--- /dev/null
+++ b/Polymorphism/Shape.h
@@ -0,0 +1,27 @@
+#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/ShapeCollection.cpp b/Polymorphism/ShapeCollection.cpp
new file mode 100644
index 0000000..f2c27f2
--- /dev/null
+++ b/Polymorphism/ShapeCollection.cpp
@@ -0,0 +1,88 @@
+#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/ShapeCollection.h b/Polymorphism/ShapeCollection.h
new file mode 100644
index 0000000..018b0af
--- /dev/null
+++ b/Polymorphism/ShapeCollection.h
@@ -0,0 +1,30 @@
+#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/Source.cpp b/Polymorphism/Source.cpp
new file mode 100644
index 0000000..2ab42e5
--- /dev/null
+++ b/Polymorphism/Source.cpp
@@ -0,0 +1,30 @@
+
+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/Triangle.cpp b/Polymorphism/Triangle.cpp
new file mode 100644
index 0000000..18dd93e
--- /dev/null
+++ b/Polymorphism/Triangle.cpp
@@ -0,0 +1,31 @@
+#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/Triangle.h b/Polymorphism/Triangle.h
new file mode 100644
index 0000000..a8a8f57
--- /dev/null
+++ b/Polymorphism/Triangle.h
@@ -0,0 +1,12 @@
+#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