Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 12 / Example_polymorphism
diff options
context:
space:
mode:
Commiteda2b5dee821c474644cd72df4d0bd93b0940ec6 (patch)
Author
 angeld55 <angel_dimitriev@abv.bg>
2022-05-17 20:15:26 +0300
Committer
 angeld55 <angel_dimitriev@abv.bg>
2022-05-17 20:15:26 +0300
Files31906c6c9c1fb1bc923cba908dc0d7f8a6ec6ab2 /Sem. 12/Example_polymorphism
Object-oriented_programming_FMI-eda2b5dee821c474644cd72df4d0bd93b0940ec6.tar
Object-oriented_programming_FMI-eda2b5dee821c474644cd72df4d0bd93b0940ec6.tar.gz
Object-oriented_programming_FMI-eda2b5dee821c474644cd72df4d0bd93b0940ec6.zip
Parentce46289350dae53570d9374506411196b669db1c (diff)
sem 12
diff --git a/Sem. 12/Example_polymorphism/Circle.cpp b/Sem. 12/Example_polymorphism/Circle.cpp
new file mode 100644
index 0000000..8226582
--- /dev/null
+++ b/Sem. 12/Example_polymorphism/Circle.cpp
@@ -0,0 +1,16 @@
+#include "Circle.h"
+
+const double PI = 3.1415;
+Circle::Circle(int x, int y, double radius) : Shape(1), radius(radius)
+{
+ setPoint(0, x, y);
+}
+
+double Circle::getArea() const
+{
+ return PI * radius * radius;
+}
+double Circle::getPer() const
+{
+ return 2 * PI * radius;
+} \ No newline at end of file
diff --git a/Sem. 12/Example_polymorphism/Circle.h b/Sem. 12/Example_polymorphism/Circle.h
new file mode 100644
index 0000000..6449fc1
--- /dev/null
+++ b/Sem. 12/Example_polymorphism/Circle.h
@@ -0,0 +1,14 @@
+#pragma once
+#include "Shape.h"
+
+class Circle : public Shape
+{
+ double radius;
+
+public:
+ Circle(int x, int y, double radius);
+
+ double getArea() const override;
+ double getPer() const override;
+
+}; \ No newline at end of file
diff --git a/Sem. 12/Example_polymorphism/Rectangle.cpp b/Sem. 12/Example_polymorphism/Rectangle.cpp
new file mode 100644
index 0000000..3fa1026
--- /dev/null
+++ b/Sem. 12/Example_polymorphism/Rectangle.cpp
@@ -0,0 +1,26 @@
+#include "Rectangle.h"
+
+Rectangle::Rectangle(int x1, int y1, int x3, int y3) : Shape(4)
+{
+ setPoint(0, x1, y1);
+ setPoint(1, x1, y3);
+ setPoint(2, x3, y3);
+ setPoint(3, x3, y1);
+}
+double Rectangle::getArea() const
+{
+ Shape::point p0 = getPointAtIndex(0);
+ Shape::point p1 = getPointAtIndex(1);
+ Shape::point p3 = getPointAtIndex(3);
+
+ return p0.getDist(p1) * p0.getDist(p3);
+
+}
+double Rectangle::getPer() const
+{
+ Shape::point p0 = getPointAtIndex(0);
+ Shape::point p1 = getPointAtIndex(1);
+ Shape::point p3 = getPointAtIndex(3);
+
+ return 2*(p0.getDist(p1) + p0.getDist(p3));
+} \ No newline at end of file
diff --git a/Sem. 12/Example_polymorphism/Rectangle.h b/Sem. 12/Example_polymorphism/Rectangle.h
new file mode 100644
index 0000000..ccdad8e
--- /dev/null
+++ b/Sem. 12/Example_polymorphism/Rectangle.h
@@ -0,0 +1,10 @@
+#pragma once
+#include "Shape.h"
+
+class Rectangle : public Shape
+{
+public:
+ Rectangle(int x1, int y1, int x3, int y3);
+ double getArea() const override;
+ double getPer() const override;
+}; \ No newline at end of file
diff --git a/Sem. 12/Example_polymorphism/Shape.cpp b/Sem. 12/Example_polymorphism/Shape.cpp
new file mode 100644
index 0000000..27bb9e9
--- /dev/null
+++ b/Sem. 12/Example_polymorphism/Shape.cpp
@@ -0,0 +1,52 @@
+#include "Shape.h"
+
+Shape::Shape(size_t pointsCount) : pointsCount(pointsCount)
+{
+ points = new point[pointsCount]; // [0,0], [0,0]....
+}
+
+void Shape::copyFrom(const Shape& other)
+{
+ points = new point[other.pointsCount];
+
+ for (int i = 0; i < other.pointsCount; i++)
+ points[i] = other.points[i];
+}
+void Shape::free()
+{
+ delete[] points;
+}
+
+Shape::Shape(const Shape& other)
+{
+ copyFrom(other);
+}
+Shape& Shape::operator= (const Shape& other)
+{
+ if (this != &other)
+ {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+Shape::~Shape()
+{
+ free();
+}
+
+const Shape::point& Shape::getPointAtIndex(size_t index) const
+{
+ if (index >= pointsCount)
+ throw std::exception("Invalid point index!");
+
+ return points[index];
+}
+
+void Shape::setPoint(size_t pointIndex, int x, int y)
+{
+ if (pointIndex >= pointsCount)
+ throw std::exception("Invalid point index!");
+
+ points[pointIndex] = point(x, y);
+}
diff --git a/Sem. 12/Example_polymorphism/Shape.h b/Sem. 12/Example_polymorphism/Shape.h
new file mode 100644
index 0000000..62f5c41
--- /dev/null
+++ b/Sem. 12/Example_polymorphism/Shape.h
@@ -0,0 +1,41 @@
+#pragma once
+#include <iostream>
+
+class Shape // Abstract class - no instances of Shape are allowed!
+{
+protected:
+ struct point
+ {
+ point() :x(0), y(0){}
+ point(int x, int y) :x(x), y(y){}
+ int x;
+ int y;
+ double getDist(const point& other) const
+ {
+ int dx = x - other.x;
+ int dy = y - other.y;
+
+ return sqrt(dx*dx + dy*dy);
+ }
+ };
+ const point& getPointAtIndex(size_t index) const;
+private:
+ point* points;
+ size_t pointsCount;
+
+ void copyFrom(const Shape& other);
+ void free();
+
+
+public:
+ Shape(size_t pointsCount);
+
+ Shape(const Shape& other);
+ Shape& operator=(const Shape& other);
+ virtual ~Shape(); //!!!!!!
+
+ void setPoint(size_t pointIndex, int x, int y);
+
+ virtual double getArea() const = 0; //pure virtual
+ virtual double getPer() const = 0; // pure virtual
+}; \ No newline at end of file
diff --git a/Sem. 12/Example_polymorphism/Triangle.cpp b/Sem. 12/Example_polymorphism/Triangle.cpp
new file mode 100644
index 0000000..7de831c
--- /dev/null
+++ b/Sem. 12/Example_polymorphism/Triangle.cpp
@@ -0,0 +1,24 @@
+#include "Triangle.h"
+
+Triangle::Triangle(int x1, int y1, int x2, int y2, int x3, int y3) : Shape(3)
+{
+ setPoint(0, x1, y1);
+ setPoint(1, x2, y2);
+ setPoint(2, x3, y3);
+}
+double Triangle::getArea() const
+{
+ Shape::point p1 = getPointAtIndex(0);
+ Shape::point p2 = getPointAtIndex(1);
+ Shape::point p3 = getPointAtIndex(2);
+
+ return abs(p1.x*p2.y + p2.x*p3.y + p3.x*p1.y - p1.y * p2.x - p2.y*p3.x - p3.y*p1.x) / 2.00;
+}
+double Triangle::getPer() const
+{
+ Shape::point p1 = getPointAtIndex(0);
+ Shape::point p2 = getPointAtIndex(1);
+ Shape::point p3 = getPointAtIndex(2);
+
+ return p1.getDist(p2) + p2.getDist(p3) + p3.getDist(p1);
+} \ No newline at end of file
diff --git a/Sem. 12/Example_polymorphism/Triangle.h b/Sem. 12/Example_polymorphism/Triangle.h
new file mode 100644
index 0000000..c157de8
--- /dev/null
+++ b/Sem. 12/Example_polymorphism/Triangle.h
@@ -0,0 +1,12 @@
+#pragma once
+
+#include "Shape.h"
+
+class Triangle : public Shape
+{
+
+public:
+ Triangle(int x1, int y1, int x2, int y2, int x3, int y3);
+ double getArea() const override;
+ double getPer() const override;
+}; \ No newline at end of file
diff --git a/Sem. 12/Example_polymorphism/main.cpp b/Sem. 12/Example_polymorphism/main.cpp
new file mode 100644
index 0000000..89a5082
--- /dev/null
+++ b/Sem. 12/Example_polymorphism/main.cpp
@@ -0,0 +1,41 @@
+#include <iostream>
+using namespace std;
+
+#include "Rectangle.h"
+#include "Circle.h"
+#include "Triangle.h"
+
+
+void printAreas(const Shape* const * shapes, size_t shapesCount)
+{
+ for (int i = 0; i < shapesCount; i++)
+ cout << shapes[i]->getArea() <<endl;
+}
+void printPers(const Shape* const * shapes, size_t shapesCount)
+{
+ for (int i = 0; i < shapesCount; i++)
+ cout << shapes[i]->getPer() << endl;
+}
+
+void freeCollection(Shape** shapes, size_t shapesCount)
+{
+ for (int i = 0; i < shapesCount; i++)
+ delete shapes[i];
+ delete[] shapes;
+}
+int main()
+{
+ Shape** arr = new Shape*[4];
+
+ arr[0] = new Rectangle(3, 4, 6, 8);
+ arr[1] = new Circle(3, 3, 4);
+ arr[2] = new Circle(1, 4, 5);
+ arr[3] = new Triangle(1, 1, 2, 2, 3, 3);
+
+ printAreas(arr, 4);
+ printPers(arr, 4);
+
+ freeCollection(arr, 4);
+
+
+} \ No newline at end of file