From 92e8d2a1aefce843e9635632cc13b9c512fea734 Mon Sep 17 00:00:00 2001 From: Angeld55 Date: Wed, 20 May 2020 03:04:17 +0300 Subject: fixed poly --- Polymorphism/Circle.cpp | 19 ------ Polymorphism/Circle.h | 16 ----- Polymorphism/Figures/Circle.hpp | 22 ------- Polymorphism/Figures/Figure.hpp | 101 -------------------------------- Polymorphism/Figures/FiguresPIC.png | Bin 39470 -> 0 bytes Polymorphism/Figures/Rectangle.hpp | 21 ------- Polymorphism/Figures/Triangle.hpp | 27 --------- Polymorphism/Figures/main.cpp | 24 -------- Polymorphism/Point.h | 18 ------ Polymorphism/Rectangle.cpp | 24 -------- Polymorphism/Rectangle.h | 11 ---- Polymorphism/Shape.cpp | 50 ---------------- Polymorphism/Shape.h | 27 --------- Polymorphism/ShapeCollection.cpp | 88 ---------------------------- Polymorphism/ShapeCollection.h | 30 ---------- Polymorphism/Shapes/Circle.cpp | 19 ++++++ Polymorphism/Shapes/Circle.h | 16 +++++ Polymorphism/Shapes/Point.h | 18 ++++++ Polymorphism/Shapes/Rectangle.cpp | 24 ++++++++ Polymorphism/Shapes/Rectangle.h | 11 ++++ Polymorphism/Shapes/Shape.cpp | 50 ++++++++++++++++ Polymorphism/Shapes/Shape.h | 27 +++++++++ Polymorphism/Shapes/ShapeCollection.cpp | 88 ++++++++++++++++++++++++++++ Polymorphism/Shapes/ShapeCollection.h | 30 ++++++++++ Polymorphism/Shapes/Source.cpp | 30 ++++++++++ Polymorphism/Shapes/Triangle.cpp | 31 ++++++++++ Polymorphism/Shapes/Triangle.h | 12 ++++ Polymorphism/Shapes/s | 1 - Polymorphism/Source.cpp | 30 ---------- Polymorphism/Triangle.cpp | 31 ---------- Polymorphism/Triangle.h | 12 ---- 31 files changed, 356 insertions(+), 552 deletions(-) delete mode 100644 Polymorphism/Circle.cpp delete mode 100644 Polymorphism/Circle.h delete mode 100644 Polymorphism/Figures/Circle.hpp delete mode 100644 Polymorphism/Figures/Figure.hpp delete mode 100644 Polymorphism/Figures/FiguresPIC.png delete mode 100644 Polymorphism/Figures/Rectangle.hpp delete mode 100644 Polymorphism/Figures/Triangle.hpp delete mode 100644 Polymorphism/Figures/main.cpp delete mode 100644 Polymorphism/Point.h delete mode 100644 Polymorphism/Rectangle.cpp delete mode 100644 Polymorphism/Rectangle.h delete mode 100644 Polymorphism/Shape.cpp delete mode 100644 Polymorphism/Shape.h delete mode 100644 Polymorphism/ShapeCollection.cpp delete mode 100644 Polymorphism/ShapeCollection.h create mode 100644 Polymorphism/Shapes/Circle.cpp create mode 100644 Polymorphism/Shapes/Circle.h create mode 100644 Polymorphism/Shapes/Point.h create mode 100644 Polymorphism/Shapes/Rectangle.cpp create mode 100644 Polymorphism/Shapes/Rectangle.h create mode 100644 Polymorphism/Shapes/Shape.cpp create mode 100644 Polymorphism/Shapes/Shape.h create mode 100644 Polymorphism/Shapes/ShapeCollection.cpp create mode 100644 Polymorphism/Shapes/ShapeCollection.h create mode 100644 Polymorphism/Shapes/Source.cpp create mode 100644 Polymorphism/Shapes/Triangle.cpp create mode 100644 Polymorphism/Shapes/Triangle.h delete mode 100644 Polymorphism/Shapes/s delete mode 100644 Polymorphism/Source.cpp delete mode 100644 Polymorphism/Triangle.cpp delete mode 100644 Polymorphism/Triangle.h (limited to 'Polymorphism') diff --git a/Polymorphism/Circle.cpp b/Polymorphism/Circle.cpp deleted file mode 100644 index 6ba083f..0000000 --- a/Polymorphism/Circle.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "Circle.h" - -Circle::Circle(int x, int y, int r) : Shape(1), radius(r) -{ - setPoint(x, y, 0); -} -double Circle::getArea() -{ - return PI * radius * radius; -} -double Circle::getPer() -{ - return 2 * PI * radius; -} -Shape* Circle::clone() -{ - Circle* newCircle = new Circle(*this); - return newCircle; -} \ No newline at end of file diff --git a/Polymorphism/Circle.h b/Polymorphism/Circle.h deleted file mode 100644 index ce355bf..0000000 --- a/Polymorphism/Circle.h +++ /dev/null @@ -1,16 +0,0 @@ -#include "Shape.h" - -const double PI = 3.14; -class Circle : public Shape -{ - - int radius; -public: - Circle(int x, int y, int radius); - - double getArea(); - double getPer(); - - Shape* clone(); - -}; \ No newline at end of file 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 -#include - -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 deleted file mode 100644 index 69a478b..0000000 Binary files a/Polymorphism/Figures/FiguresPIC.png and /dev/null differ 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()<getArea()< -struct Point -{ - int x; - int y; - Point(int x, int y) - { - this->x = x; - this->y = y; - } - Point() : Point(0,0) - {} - double getDist(const Point& other) const - { - return sqrt((x - other.x)*(x - other.x) + (y - other.y) * (y - other.y)); - } -}; \ No newline at end of file diff --git a/Polymorphism/Rectangle.cpp b/Polymorphism/Rectangle.cpp deleted file mode 100644 index 8f9d1fb..0000000 --- a/Polymorphism/Rectangle.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "Rectangle.h" - - -Rectangle::Rectangle(int x1, int y1, int x3, int y3) : Shape(4) -{ - setPoint(x1, y1, 0); - setPoint(x3, y3, 2); - setPoint(x1, y3, 1); - setPoint(x3, y1, 3); -} - -double Rectangle::getPer() -{ - return 2 * (getPoint(0).getDist(getPoint(1)) + getPoint(1).getDist(getPoint(2)) ); -} -double Rectangle::getArea() -{ - return getPoint(0).getDist(getPoint(1)) * getPoint(1).getDist(getPoint(2)); -} -Shape* Rectangle::clone() -{ - Rectangle* newRect = new Rectangle(*this); - return newRect; -} \ No newline at end of file diff --git a/Polymorphism/Rectangle.h b/Polymorphism/Rectangle.h deleted file mode 100644 index 7c7e3d0..0000000 --- a/Polymorphism/Rectangle.h +++ /dev/null @@ -1,11 +0,0 @@ -#include "Shape.h" -class Rectangle : public Shape -{ -public: - Rectangle(int x1, int y1, int x3, int y3); - - double getPer(); - double getArea(); - - Shape* clone(); -}; \ No newline at end of file diff --git a/Polymorphism/Shape.cpp b/Polymorphism/Shape.cpp deleted file mode 100644 index 787ec82..0000000 --- a/Polymorphism/Shape.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include "Shape.h" - -Shape::Shape(int pointsCount) -{ - points = new Point[pointsCount]; - this->pointsCount = pointsCount; -} - -void Shape::setPoint(int x, int y, int ind) -{ - Point current(x, y); - points[ind] = current; - -} - -const Point& Shape::getPoint(int ind) -{ - return points[ind]; -} - -void Shape::CopyFrom(const Shape& other) -{ - points = new Point[other.pointsCount]; - pointsCount = other.pointsCount; - - for (int i = 0; i < pointsCount; i++) - points[i] = other.points[i]; -} -void Shape::Free() -{ - delete[] points; - pointsCount = 0; -} -Shape::Shape(const Shape& other) -{ - CopyFrom(other); -} -Shape& Shape::operator=(const Shape& other) -{ - if (this != &other) - { - Free(); - CopyFrom(other); - } - return *this; -} -Shape::~Shape() -{ - Free(); -} diff --git a/Polymorphism/Shape.h b/Polymorphism/Shape.h deleted file mode 100644 index fbce027..0000000 --- a/Polymorphism/Shape.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include "Point.h" - -class Shape -{ - Point* points; - int pointsCount; - - void CopyFrom(const Shape& other); - void Free(); - -public: - Shape(int pointsCount); - Shape(const Shape& other); - Shape& operator=(const Shape& other); - virtual ~Shape(); - - void setPoint(int x, int y, int ind); - const Point& getPoint(int ind); - - virtual double getPer() =0; - virtual double getArea() = 0; - - virtual Shape* clone() = 0; - -}; \ No newline at end of file diff --git a/Polymorphism/ShapeCollection.cpp b/Polymorphism/ShapeCollection.cpp deleted file mode 100644 index f2c27f2..0000000 --- a/Polymorphism/ShapeCollection.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include "ShapeCollection.h" - -ShapeCollection::ShapeCollection() -{ - count = 0; - capacity = 8; - - data = new Shape*[capacity]; -} - -void ShapeCollection::AddCircle(int x1, int y1, int r) -{ - Circle* current = new Circle(x1, y1, r); - Add(current); - -} -void ShapeCollection::AddTriagnle(int x1, int y1, int x2, int y2, int x3, int y3) -{ - Triangle* current = new Triangle(x1, y1, x2, y2, x3, y3); - Add(current); -} -void ShapeCollection::AddRect(int x1, int y1, int x2, int y2) -{ - Rectangle* current = new Rectangle(x1, y1, x2, y2); - Add(current); -} -void ShapeCollection::Add(Shape* currentShape) -{ - if (count == capacity) - Resize(); - data[count++] = currentShape; -} -void ShapeCollection::Free() -{ - for (int i = 0; i < count; i++) - delete data[i]; - - delete[] data; -} -void ShapeCollection::CopyFrom(const ShapeCollection& other) -{ - data = new Shape*[other.count]; - capacity = other.capacity; - count = other.count; - for (int i = 0; i < other.count; i++) - data[i] = other.data[i]->clone(); - -} -ShapeCollection::ShapeCollection(const ShapeCollection& other) -{ - CopyFrom(other); -} -ShapeCollection& ShapeCollection::operator = (const ShapeCollection& other) -{ - if (this != &other) - { - Free(); - CopyFrom(other); - } - return *this; -} -ShapeCollection::~ShapeCollection() -{ - Free(); -} -void ShapeCollection::Resize() -{ - Shape** temp = new Shape*[capacity * 2]; - capacity *= 2; - for (int i = 0; i < count; i++) - temp[i] = data[i]; - delete[] data; - data = temp; - - -} -void ShapeCollection::PrintAllAreas() -{ - for (int i = 0; i < count; i++) - std::cout << data[i]->getArea() << " "; - std::cout<getPer() << " "; - std::cout << std::endl; -} \ No newline at end of file diff --git a/Polymorphism/ShapeCollection.h b/Polymorphism/ShapeCollection.h deleted file mode 100644 index 018b0af..0000000 --- a/Polymorphism/ShapeCollection.h +++ /dev/null @@ -1,30 +0,0 @@ -#include "Circle.h" -#include "Rectangle.h" -#include "Triangle.h" - -class ShapeCollection -{ - Shape** data; - int count; - int capacity; - - void Resize(); - - void Add(Shape* currentShape); - - void CopyFrom(const ShapeCollection& other); - void Free(); -public: - ShapeCollection(); - - ShapeCollection(const ShapeCollection& other); - ShapeCollection& operator=(const ShapeCollection& other); - ~ShapeCollection(); - - void AddCircle(int x1,int x2,int r); - void AddTriagnle(int x1,int y1,int x2,int y2,int x3,int y3); - void AddRect(int x1, int y1, int x2, int y2); - - void PrintAllAreas(); - void PrintAllPers(); -}; \ No newline at end of file diff --git a/Polymorphism/Shapes/Circle.cpp b/Polymorphism/Shapes/Circle.cpp new file mode 100644 index 0000000..e625123 --- /dev/null +++ b/Polymorphism/Shapes/Circle.cpp @@ -0,0 +1,19 @@ +#include "Circle.h" + +Circle::Circle(int x, int y, int r) : Shape(1), radius(r) +{ + setPoint(x, y, 0); +} +double Circle::getArea() +{ + return PI * radius * radius; +} +double Circle::getPer() +{ + return 2 * PI * radius; +} +Shape* Circle::clone() +{ + Circle* newCircle = new Circle(*this); + return newCircle; +} \ No newline at end of file diff --git a/Polymorphism/Shapes/Circle.h b/Polymorphism/Shapes/Circle.h new file mode 100644 index 0000000..c648690 --- /dev/null +++ b/Polymorphism/Shapes/Circle.h @@ -0,0 +1,16 @@ +#include "Shape.h" + +const double PI = 3.14; +class Circle : public Shape +{ + + int radius; +public: + Circle(int x, int y, int radius); + + double getArea(); + double getPer(); + + Shape* clone(); + +}; \ No newline at end of file diff --git a/Polymorphism/Shapes/Point.h b/Polymorphism/Shapes/Point.h new file mode 100644 index 0000000..acacb85 --- /dev/null +++ b/Polymorphism/Shapes/Point.h @@ -0,0 +1,18 @@ +#pragma once +#include +struct Point +{ + int x; + int y; + Point(int x, int y) + { + this->x = x; + this->y = y; + } + Point() : Point(0,0) + {} + double getDist(const Point& other) const + { + return sqrt((x - other.x)*(x - other.x) + (y - other.y) * (y - other.y)); + } +}; \ No newline at end of file diff --git a/Polymorphism/Shapes/Rectangle.cpp b/Polymorphism/Shapes/Rectangle.cpp new file mode 100644 index 0000000..b684c50 --- /dev/null +++ b/Polymorphism/Shapes/Rectangle.cpp @@ -0,0 +1,24 @@ +#include "Rectangle.h" + + +Rectangle::Rectangle(int x1, int y1, int x3, int y3) : Shape(4) +{ + setPoint(x1, y1, 0); + setPoint(x3, y3, 2); + setPoint(x1, y3, 1); + setPoint(x3, y1, 3); +} + +double Rectangle::getPer() +{ + return 2 * (getPoint(0).getDist(getPoint(1)) + getPoint(1).getDist(getPoint(2)) ); +} +double Rectangle::getArea() +{ + return getPoint(0).getDist(getPoint(1)) * getPoint(1).getDist(getPoint(2)); +} +Shape* Rectangle::clone() +{ + Rectangle* newRect = new Rectangle(*this); + return newRect; +} \ No newline at end of file diff --git a/Polymorphism/Shapes/Rectangle.h b/Polymorphism/Shapes/Rectangle.h new file mode 100644 index 0000000..681a021 --- /dev/null +++ b/Polymorphism/Shapes/Rectangle.h @@ -0,0 +1,11 @@ +#include "Shape.h" +class Rectangle : public Shape +{ +public: + Rectangle(int x1, int y1, int x3, int y3); + + double getPer(); + double getArea(); + + Shape* clone(); +}; \ No newline at end of file diff --git a/Polymorphism/Shapes/Shape.cpp b/Polymorphism/Shapes/Shape.cpp new file mode 100644 index 0000000..0751108 --- /dev/null +++ b/Polymorphism/Shapes/Shape.cpp @@ -0,0 +1,50 @@ +#include "Shape.h" + +Shape::Shape(int pointsCount) +{ + points = new Point[pointsCount]; + this->pointsCount = pointsCount; +} + +void Shape::setPoint(int x, int y, int ind) +{ + Point current(x, y); + points[ind] = current; + +} + +const Point& Shape::getPoint(int ind) +{ + return points[ind]; +} + +void Shape::CopyFrom(const Shape& other) +{ + points = new Point[other.pointsCount]; + pointsCount = other.pointsCount; + + for (int i = 0; i < pointsCount; i++) + points[i] = other.points[i]; +} +void Shape::Free() +{ + delete[] points; + pointsCount = 0; +} +Shape::Shape(const Shape& other) +{ + CopyFrom(other); +} +Shape& Shape::operator=(const Shape& other) +{ + if (this != &other) + { + Free(); + CopyFrom(other); + } + return *this; +} +Shape::~Shape() +{ + Free(); +} diff --git a/Polymorphism/Shapes/Shape.h b/Polymorphism/Shapes/Shape.h new file mode 100644 index 0000000..f31e42e --- /dev/null +++ b/Polymorphism/Shapes/Shape.h @@ -0,0 +1,27 @@ +#pragma once + +#include "Point.h" + +class Shape +{ + Point* points; + int pointsCount; + + void CopyFrom(const Shape& other); + void Free(); + +public: + Shape(int pointsCount); + Shape(const Shape& other); + Shape& operator=(const Shape& other); + virtual ~Shape(); + + void setPoint(int x, int y, int ind); + const Point& getPoint(int ind); + + virtual double getPer() =0; + virtual double getArea() = 0; + + virtual Shape* clone() = 0; + +}; \ No newline at end of file diff --git a/Polymorphism/Shapes/ShapeCollection.cpp b/Polymorphism/Shapes/ShapeCollection.cpp new file mode 100644 index 0000000..8fcad3c --- /dev/null +++ b/Polymorphism/Shapes/ShapeCollection.cpp @@ -0,0 +1,88 @@ +#include "ShapeCollection.h" + +ShapeCollection::ShapeCollection() +{ + count = 0; + capacity = 8; + + data = new Shape*[capacity]; +} + +void ShapeCollection::AddCircle(int x1, int y1, int r) +{ + Circle* current = new Circle(x1, y1, r); + Add(current); + +} +void ShapeCollection::AddTriagnle(int x1, int y1, int x2, int y2, int x3, int y3) +{ + Triangle* current = new Triangle(x1, y1, x2, y2, x3, y3); + Add(current); +} +void ShapeCollection::AddRect(int x1, int y1, int x2, int y2) +{ + Rectangle* current = new Rectangle(x1, y1, x2, y2); + Add(current); +} +void ShapeCollection::Add(Shape* currentShape) +{ + if (count == capacity) + Resize(); + data[count++] = currentShape; +} +void ShapeCollection::Free() +{ + for (int i = 0; i < count; i++) + delete data[i]; + + delete[] data; +} +void ShapeCollection::CopyFrom(const ShapeCollection& other) +{ + data = new Shape*[other.count]; + capacity = other.capacity; + count = other.count; + for (int i = 0; i < other.count; i++) + data[i] = other.data[i]->clone(); + +} +ShapeCollection::ShapeCollection(const ShapeCollection& other) +{ + CopyFrom(other); +} +ShapeCollection& ShapeCollection::operator = (const ShapeCollection& other) +{ + if (this != &other) + { + Free(); + CopyFrom(other); + } + return *this; +} +ShapeCollection::~ShapeCollection() +{ + Free(); +} +void ShapeCollection::Resize() +{ + Shape** temp = new Shape*[capacity * 2]; + capacity *= 2; + for (int i = 0; i < count; i++) + temp[i] = data[i]; + delete[] data; + data = temp; + + +} +void ShapeCollection::PrintAllAreas() +{ + for (int i = 0; i < count; i++) + std::cout << data[i]->getArea() << " "; + std::cout<getPer() << " "; + std::cout << std::endl; +} \ No newline at end of file diff --git a/Polymorphism/Shapes/ShapeCollection.h b/Polymorphism/Shapes/ShapeCollection.h new file mode 100644 index 0000000..36f6092 --- /dev/null +++ b/Polymorphism/Shapes/ShapeCollection.h @@ -0,0 +1,30 @@ +#include "Circle.h" +#include "Rectangle.h" +#include "Triangle.h" + +class ShapeCollection +{ + Shape** data; + int count; + int capacity; + + void Resize(); + + void Add(Shape* currentShape); + + void CopyFrom(const ShapeCollection& other); + void Free(); +public: + ShapeCollection(); + + ShapeCollection(const ShapeCollection& other); + ShapeCollection& operator=(const ShapeCollection& other); + ~ShapeCollection(); + + void AddCircle(int x1,int x2,int r); + void AddTriagnle(int x1,int y1,int x2,int y2,int x3,int y3); + void AddRect(int x1, int y1, int x2, int y2); + + void PrintAllAreas(); + void PrintAllPers(); +}; \ No newline at end of file diff --git a/Polymorphism/Shapes/Source.cpp b/Polymorphism/Shapes/Source.cpp new file mode 100644 index 0000000..3381111 --- /dev/null +++ b/Polymorphism/Shapes/Source.cpp @@ -0,0 +1,30 @@ + +using namespace std; +#include +#include +#include "ShapeCollection.h" + +int main() +{ + ShapeCollection sh; + + sh.AddCircle(3, 3, 5); + sh.AddTriagnle(1, 4, 2, 11, 7, 4); + sh.AddRect(1, 1, 4, 4); + + sh.PrintAllAreas(); + +/* lamda fucntion example + std::vector s; + s.push_back(Circle(3, 4, 4)); + s.push_back(Circle(1, 2, 3)); + + std::sort(s.begin(), s.end(), + [](Circle& lhs, Circle& rhs) -> bool + { + return lhs.getArea() < rhs.getArea(); + });*/ + + + +} \ No newline at end of file diff --git a/Polymorphism/Shapes/Triangle.cpp b/Polymorphism/Shapes/Triangle.cpp new file mode 100644 index 0000000..545e2a6 --- /dev/null +++ b/Polymorphism/Shapes/Triangle.cpp @@ -0,0 +1,31 @@ +#include "Triangle.h" + +Triangle::Triangle(int x1, int y1, int x2, int y2, int x3, int y3) : Shape(3) +{ + setPoint(x1, y1, 0); + setPoint(x2, y2, 1); + setPoint(x3, y3, 2); +} + +double Triangle::getArea() +{ + double sideA = getPoint(0).getDist(getPoint(1)); + double sideB = getPoint(1).getDist(getPoint(2)); + double sideC = getPoint(2).getDist(getPoint(0)); + + double halfPer = getPer() / 2; + + return sqrt(halfPer * (halfPer - sideA) * + (halfPer - sideB) * (halfPer - sideC)); +} +double Triangle::getPer() +{ + return getPoint(0).getDist(getPoint(1)) + + getPoint(1).getDist(getPoint(2)) + + getPoint(2).getDist(getPoint(0)); +} +Shape* Triangle::clone() +{ + Triangle* newTriangle = new Triangle(*this); + return newTriangle; +} \ No newline at end of file diff --git a/Polymorphism/Shapes/Triangle.h b/Polymorphism/Shapes/Triangle.h new file mode 100644 index 0000000..b038e80 --- /dev/null +++ b/Polymorphism/Shapes/Triangle.h @@ -0,0 +1,12 @@ +#include "Shape.h" + +class Triangle : public Shape +{ +public: + Triangle(int x1, int y1, int x2, int y2, int x3, int y3); + + double getArea(); + double getPer(); + + Shape* clone(); +}; \ No newline at end of file diff --git a/Polymorphism/Shapes/s b/Polymorphism/Shapes/s deleted file mode 100644 index b478595..0000000 --- a/Polymorphism/Shapes/s +++ /dev/null @@ -1 +0,0 @@ -s diff --git a/Polymorphism/Source.cpp b/Polymorphism/Source.cpp deleted file mode 100644 index 2ab42e5..0000000 --- a/Polymorphism/Source.cpp +++ /dev/null @@ -1,30 +0,0 @@ - -using namespace std; -#include -#include -#include "ShapeCollection.h" - -int main() -{ - ShapeCollection sh; - - sh.AddCircle(3, 3, 5); - sh.AddTriagnle(1, 4, 2, 11, 7, 4); - sh.AddRect(1, 1, 4, 4); - - sh.PrintAllAreas(); - -/* lamda fucntion example - std::vector s; - s.push_back(Circle(3, 4, 4)); - s.push_back(Circle(1, 2, 3)); - - std::sort(s.begin(), s.end(), - [](Circle& lhs, Circle& rhs) -> bool - { - return lhs.getArea() < rhs.getArea(); - });*/ - - - -} \ No newline at end of file diff --git a/Polymorphism/Triangle.cpp b/Polymorphism/Triangle.cpp deleted file mode 100644 index 18dd93e..0000000 --- a/Polymorphism/Triangle.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "Triangle.h" - -Triangle::Triangle(int x1, int y1, int x2, int y2, int x3, int y3) : Shape(3) -{ - setPoint(x1, y1, 0); - setPoint(x2, y2, 1); - setPoint(x3, y3, 2); -} - -double Triangle::getArea() -{ - double sideA = getPoint(0).getDist(getPoint(1)); - double sideB = getPoint(1).getDist(getPoint(2)); - double sideC = getPoint(2).getDist(getPoint(0)); - - double halfPer = getPer() / 2; - - return sqrt(halfPer * (halfPer - sideA) * - (halfPer - sideB) * (halfPer - sideC)); -} -double Triangle::getPer() -{ - return getPoint(0).getDist(getPoint(1)) + - getPoint(1).getDist(getPoint(2)) + - getPoint(2).getDist(getPoint(0)); -} -Shape* Triangle::clone() -{ - Triangle* newTriangle = new Triangle(*this); - return newTriangle; -} \ No newline at end of file diff --git a/Polymorphism/Triangle.h b/Polymorphism/Triangle.h deleted file mode 100644 index a8a8f57..0000000 --- a/Polymorphism/Triangle.h +++ /dev/null @@ -1,12 +0,0 @@ -#include "Shape.h" - -class Triangle : public Shape -{ -public: - Triangle(int x1, int y1, int x2, int y2, int x3, int y3); - - double getArea(); - double getPer(); - - Shape* clone(); -}; \ No newline at end of file -- cgit v1.3.1-23-gd82d