diff options
Add files via upload
4 files changed, 81 insertions, 0 deletions
| -rw-r--r-- | Polymorphism/Circle.hpp | 20 | ||||
| -rw-r--r-- | Polymorphism/Rectangle.hpp | 20 | ||||
| -rw-r--r-- | Polymorphism/Triangle.hpp | 21 | ||||
| -rw-r--r-- | Polymorphism/main.cpp | 20 |
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()<<endl;
+ cout << arr[i]->getArea()<<endl;
+ }
+
+
+}
+
+
|
