diff options
fixed poly
6 files changed, 0 insertions, 195 deletions
| -rw-r--r-- | Polymorphism/Figures/Circle.hpp | 22 | ||||
| -rw-r--r-- | Polymorphism/Figures/Figure.hpp | 101 | ||||
| -rw-r--r-- | Polymorphism/Figures/FiguresPIC.png | bin | 39470 -> 0 bytes | |||
| -rw-r--r-- | Polymorphism/Figures/Rectangle.hpp | 21 | ||||
| -rw-r--r-- | Polymorphism/Figures/Triangle.hpp | 27 | ||||
| -rw-r--r-- | Polymorphism/Figures/main.cpp | 24 |
diff --git a/Polymorphism/Figures/Circle.hpp b/Polymorphism/Figures/Circle.hpp deleted file mode 100644 index 282ed36..0000000 --- a/Polymorphism/Figures/Circle.hpp +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once
-#include "Figure.hpp"
-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;
- }
-};
diff --git a/Polymorphism/Figures/Figure.hpp b/Polymorphism/Figures/Figure.hpp deleted file mode 100644 index cd13b7d..0000000 --- a/Polymorphism/Figures/Figure.hpp +++ /dev/null @@ -1,101 +0,0 @@ -#pragma once - -#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]; - -} diff --git a/Polymorphism/Figures/FiguresPIC.png b/Polymorphism/Figures/FiguresPIC.png Binary files differdeleted file mode 100644 index 69a478b..0000000 --- a/Polymorphism/Figures/FiguresPIC.png +++ /dev/null diff --git a/Polymorphism/Figures/Rectangle.hpp b/Polymorphism/Figures/Rectangle.hpp deleted file mode 100644 index e1a4ea8..0000000 --- a/Polymorphism/Figures/Rectangle.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once
-#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));
- }
-};
diff --git a/Polymorphism/Figures/Triangle.hpp b/Polymorphism/Figures/Triangle.hpp deleted file mode 100644 index 5d16111..0000000 --- a/Polymorphism/Figures/Triangle.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once
-
-#include "Figure.hpp"
-class Triangle : public Figure
-{
-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)));
- }
-};
diff --git a/Polymorphism/Figures/main.cpp b/Polymorphism/Figures/main.cpp deleted file mode 100644 index 2b4eb23..0000000 --- a/Polymorphism/Figures/main.cpp +++ /dev/null @@ -1,24 +0,0 @@ -
-#include "Figure.hpp"
-#include "Triangle.hpp"
-#include "Rectangle.hpp"
-#include "Circle.hpp"
-
-
-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;
- }
-
-
-}
-
-
|
