Website Summary Files Commits Branches @commit @diff @stats
path: root / Polymorphism / Figures
diff options
context:
space:
mode:
Commitdad8df13e2324b0755052dafeb79589e7f82456e (patch)
Author
 Angeld55 <achi_d@abv.bg>
2019-09-19 21:18:52 +0300
Committer
 GitHub <noreply@github.com>
2019-09-19 21:18:52 +0300
Fileseb8facea1f3a033609d143c0fa6ef57d375a2634 /Polymorphism/Figures
Object-oriented_programming_FMI-dad8df13e2324b0755052dafeb79589e7f82456e.tar
Object-oriented_programming_FMI-dad8df13e2324b0755052dafeb79589e7f82456e.tar.gz
Object-oriented_programming_FMI-dad8df13e2324b0755052dafeb79589e7f82456e.zip
Parente318d44b1fbdc4daefbbfe631a45087634add6ee (diff)
Update and rename Polymorphism/Figure.hpp to Polymorphism/Figures/Figure.hpp
1 files changed, 125 insertions, 0 deletions
-rw-r--r--Polymorphism/Figures/Figure.hpp125
diff --git a/Polymorphism/Figures/Figure.hpp b/Polymorphism/Figures/Figure.hpp
new file mode 100644
index 0000000..8381ae7
--- /dev/null
+++ b/Polymorphism/Figures/Figure.hpp
@@ -0,0 +1,125 @@
+#include<iostream>
+#include <cmath>
+
+using namespace std;
+const double PI = 3.1415;
+
+struct Point
+{
+ int x;
+ int y;
+ Point(int x, int y)
+ {
+ this->x = x;
+ this->y = y;
+ }
+ Point()
+ {
+ x = y = 0;
+ }
+ double getDistTo(const Point& other) const
+ {
+ return sqrt((x - other.x)*(x - other.x) + (y - other.y)*(y - other.y));
+ }
+};
+
+class Figure
+{
+
+ Point* points;
+ int pointsCount;
+ void CopyFrom(const Figure& other);
+ void Free();
+public:
+ Figure(int pointsCount);
+ Figure(const Figure& other);
+ Figure& operator=(const Figure& other);
+ ~Figure();
+ void setPoint(int ind, int x, int y);
+ const Point& getPoint(int ind);
+
+ virtual double getPer() = 0;
+ virtual double getArea()= 0;
+
+
+};
+
+void Figure::CopyFrom(const Figure& other)
+{
+ points = new Point[other.pointsCount];
+ for (int i = 0; i < other.pointsCount; i++)
+ points[i] = other.points[i];
+ pointsCount = other.pointsCount;
+}
+void Figure::Free()
+{
+ delete[] points;
+ pointsCount = 0;
+}
+Figure::Figure(const Figure& other)
+{
+ CopyFrom(other);
+}
+Figure& Figure::operator=(const Figure& other)
+{
+ if (this != &other)
+ {
+ Free();
+ CopyFrom(other);
+ }
+ return *this;
+}
+Figure::~Figure()
+{
+ Free();
+}
+Figure::Figure(int pointsCount)
+{
+ points = new Point[pointsCount];
+ this->pointsCount = pointsCount;
+}
+
+void Figure::setPoint(int ind, int x, int y)
+{
+ if (ind < 0 || ind >= pointsCount)
+ return;
+ points[ind].x = x;
+ points[ind].y = y;
+}
+
+const Point& Figure::getPoint(int ind)
+{
+ if (ind < 0 || ind >= pointsCount)
+ throw "Invalid index";
+ return points[ind];
+
+}
+
+
+
+int main()
+{
+ Figure** arr= new Figure*[3];
+ arr[0] = new Triangle(15, 15, 15, 30, 50, 30);
+ arr[1] = new Rectangle(15, 15, 50, 30);
+ arr[2] = new Circle(1, 2, 9);
+
+ for (int i = 0; i < 3; i++)
+ {
+ cout << arr[i]->getPer()<<endl;
+ cout << arr[i]->getArea()<<endl;
+ }
+
+
+}
+
+
+
+
+
+
+
+
+
+
+