From 29b40528c6b7fc5ac56acc6dc6054b5270349c73 Mon Sep 17 00:00:00 2001 From: Angeld55 Date: Thu, 19 Sep 2019 21:17:07 +0300 Subject: Add files via upload --- Polymorphism/Circle.hpp | 20 ++++++++++++++++++++ Polymorphism/Rectangle.hpp | 20 ++++++++++++++++++++ Polymorphism/Triangle.hpp | 21 +++++++++++++++++++++ Polymorphism/main.cpp | 20 ++++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 Polymorphism/Circle.hpp create mode 100644 Polymorphism/Rectangle.hpp create mode 100644 Polymorphism/Triangle.hpp create mode 100644 Polymorphism/main.cpp (limited to 'Polymorphism') diff --git a/Polymorphism/Circle.hpp b/Polymorphism/Circle.hpp new file mode 100644 index 0000000..f36d379 --- /dev/null +++ b/Polymorphism/Circle.hpp @@ -0,0 +1,20 @@ +class Circle : public Figure +{ + int radius; +public: + Circle(int x, int y, int r) : Figure(1) + { + setPoint(0, x, y); + if (r < 0) + r = 0; + radius = r; + } + double getPer() + { + return 2 * PI * radius; + } + double getArea() + { + return radius*radius*PI; + } +}; \ No newline at end of file diff --git a/Polymorphism/Rectangle.hpp b/Polymorphism/Rectangle.hpp new file mode 100644 index 0000000..72d1e7f --- /dev/null +++ b/Polymorphism/Rectangle.hpp @@ -0,0 +1,20 @@ +#include "Figure.hpp" +class Rectangle : public Figure +{ +public: + Rectangle(int x1, int y1, int x2, int y2) : Figure(4) + { + setPoint(0, x1, y1); + setPoint(1, x2, y1); + setPoint(2, x2, y2); + setPoint(3, x1, y2); + } + double getPer() + { + return 2 * (getPoint(0).getDistTo(getPoint(1)) + getPoint(0).getDistTo(getPoint(3))); + } + double getArea() + { + return getPoint(0).getDistTo(getPoint(1))* getPoint(1).getDistTo(getPoint(2)); + } +}; \ No newline at end of file diff --git a/Polymorphism/Triangle.hpp b/Polymorphism/Triangle.hpp new file mode 100644 index 0000000..8372464 --- /dev/null +++ b/Polymorphism/Triangle.hpp @@ -0,0 +1,21 @@ +public: + Triangle(int x1, int y1, int x2, int y2, int x3, int y3) :Figure(3) + { + setPoint(0, x1, y1); + setPoint(1, x2, y2); + setPoint(2, x3, y3); + } + double getSide(int side) + { + return getPoint(side).getDistTo(getPoint((side+1)%3)); + } + double getPer() + { + return getSide(0)+getSide(1)+getSide(2); + } + double getArea() + { + double halfP = getPer()/2; + return sqrt(halfP * (halfP-getSide(0))*(halfP-getSide(1))*(halfP-getSide(2))); + } +}; \ No newline at end of file diff --git a/Polymorphism/main.cpp b/Polymorphism/main.cpp new file mode 100644 index 0000000..39336c3 --- /dev/null +++ b/Polymorphism/main.cpp @@ -0,0 +1,20 @@ +#include "Figure.h" + + +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()<getArea()<