From cbe098fdaf6509b6a3c8bd33b3064d8f620dd58c Mon Sep 17 00:00:00 2001 From: angeld55 Date: Fri, 28 May 2021 16:45:24 +0300 Subject: sem 14 --- Sem. 14/Collection/ShapesCollection.cpp | 67 +++++++++++++++++++++++++++ Sem. 14/Collection/ShapesCollection.h | 33 +++++++++++++ Sem. 14/Shapes/Circle.cpp | 22 +++++++++ Sem. 14/Shapes/Circle.h | 15 ++++++ Sem. 14/Shapes/Rectangle.cpp | 32 +++++++++++++ Sem. 14/Shapes/Rectangle.h | 13 ++++++ Sem. 14/Shapes/Rhombus.cpp | 25 ++++++++++ Sem. 14/Shapes/Rhombus.h | 12 +++++ Sem. 14/Shapes/Shape.cpp | 52 +++++++++++++++++++++ Sem. 14/Shapes/Shape.h | 43 +++++++++++++++++ Sem. 14/Shapes/Square.cpp | 19 ++++++++ Sem. 14/Shapes/Square.h | 13 ++++++ Sem. 14/Shapes/Triangle.cpp | 30 ++++++++++++ Sem. 14/Shapes/Triangle.h | 14 ++++++ Sem. 14/ShapesCollection/Circle.cpp | 22 --------- Sem. 14/ShapesCollection/Circle.h | 15 ------ Sem. 14/ShapesCollection/Rectangle.cpp | 32 ------------- Sem. 14/ShapesCollection/Rectangle.h | 13 ------ Sem. 14/ShapesCollection/Rhombus.cpp | 25 ---------- Sem. 14/ShapesCollection/Rhombus.h | 12 ----- Sem. 14/ShapesCollection/Shape.cpp | 52 --------------------- Sem. 14/ShapesCollection/Shape.h | 43 ----------------- Sem. 14/ShapesCollection/ShapesCollection.cpp | 67 --------------------------- Sem. 14/ShapesCollection/ShapesCollection.h | 33 ------------- Sem. 14/ShapesCollection/Square.cpp | 19 -------- Sem. 14/ShapesCollection/Square.h | 13 ------ Sem. 14/ShapesCollection/Triangle.cpp | 30 ------------ Sem. 14/ShapesCollection/Triangle.h | 14 ------ Sem. 14/ShapesCollection/main.cpp | 18 ------- Sem. 14/main.cpp | 18 +++++++ 30 files changed, 408 insertions(+), 408 deletions(-) create mode 100644 Sem. 14/Collection/ShapesCollection.cpp create mode 100644 Sem. 14/Collection/ShapesCollection.h create mode 100644 Sem. 14/Shapes/Circle.cpp create mode 100644 Sem. 14/Shapes/Circle.h create mode 100644 Sem. 14/Shapes/Rectangle.cpp create mode 100644 Sem. 14/Shapes/Rectangle.h create mode 100644 Sem. 14/Shapes/Rhombus.cpp create mode 100644 Sem. 14/Shapes/Rhombus.h create mode 100644 Sem. 14/Shapes/Shape.cpp create mode 100644 Sem. 14/Shapes/Shape.h create mode 100644 Sem. 14/Shapes/Square.cpp create mode 100644 Sem. 14/Shapes/Square.h create mode 100644 Sem. 14/Shapes/Triangle.cpp create mode 100644 Sem. 14/Shapes/Triangle.h delete mode 100644 Sem. 14/ShapesCollection/Circle.cpp delete mode 100644 Sem. 14/ShapesCollection/Circle.h delete mode 100644 Sem. 14/ShapesCollection/Rectangle.cpp delete mode 100644 Sem. 14/ShapesCollection/Rectangle.h delete mode 100644 Sem. 14/ShapesCollection/Rhombus.cpp delete mode 100644 Sem. 14/ShapesCollection/Rhombus.h delete mode 100644 Sem. 14/ShapesCollection/Shape.cpp delete mode 100644 Sem. 14/ShapesCollection/Shape.h delete mode 100644 Sem. 14/ShapesCollection/ShapesCollection.cpp delete mode 100644 Sem. 14/ShapesCollection/ShapesCollection.h delete mode 100644 Sem. 14/ShapesCollection/Square.cpp delete mode 100644 Sem. 14/ShapesCollection/Square.h delete mode 100644 Sem. 14/ShapesCollection/Triangle.cpp delete mode 100644 Sem. 14/ShapesCollection/Triangle.h delete mode 100644 Sem. 14/ShapesCollection/main.cpp create mode 100644 Sem. 14/main.cpp diff --git a/Sem. 14/Collection/ShapesCollection.cpp b/Sem. 14/Collection/ShapesCollection.cpp new file mode 100644 index 0000000..0fe828c --- /dev/null +++ b/Sem. 14/Collection/ShapesCollection.cpp @@ -0,0 +1,67 @@ +#include "ShapesCollection.h" + +void ShapesCollection::addCircle(double x, double y, double radius) +{ + Circle* ptr = new Circle(x, y, radius); + collection.push_back(ptr); +} +void ShapesCollection::addRectangle(double x1, double y1, double x3, double y3) +{ + Rectangle* ptr = new Rectangle(x1, y1, x3, y3); + collection.push_back(ptr); +} +void ShapesCollection::addTriangle(double x1, double y1, double x2, double y2, double x3, double y3) +{ + Triangle* ptr = new Triangle(x1, y1, x2, y2, x3, y3); + collection.push_back(ptr); +} +void ShapesCollection::addRhombus(double x1, double y1, double x2, double y2) +{ + collection.push_back(new Rhombus(x1, y1, x2, y2)); +} + +void ShapesCollection::addSquare(double x1, double y1, double sideLength) +{ + collection.push_back(new Square(x1, y1, sideLength)); +} + +void ShapesCollection::printAreas() +{ + for (int i = 0; i < collection.size(); i++) + std::cout << collection[i]->getArea() << " " << std::endl; +} +void ShapesCollection::printPers() +{ + for (int i = 0; i < collection.size(); i++) + std::cout << collection[i]->getPer() << " " << std::endl; +} + +void ShapesCollection::copyFrom(const ShapesCollection& other) +{ + for (int i = 0; i < other.collection.size(); i++) + collection.push_back(other.collection[i]->clone()); +} +void ShapesCollection::free() +{ + for (int i = 0; i < collection.size(); i++) + delete collection[i]; + collection.clear(); +} + +ShapesCollection::ShapesCollection(const ShapesCollection& other) +{ + copyFrom(other); +} +ShapesCollection& ShapesCollection::operator= (const ShapesCollection& other) +{ + if (this != &other) + { + free(); + copyFrom(other); + } + return *this; +} +ShapesCollection::~ShapesCollection() +{ + free(); +} \ No newline at end of file diff --git a/Sem. 14/Collection/ShapesCollection.h b/Sem. 14/Collection/ShapesCollection.h new file mode 100644 index 0000000..0a9638d --- /dev/null +++ b/Sem. 14/Collection/ShapesCollection.h @@ -0,0 +1,33 @@ +#include "../Shapes/Circle.h" +#include "../Shapes/Rectangle.h" +#include "../Shapes/Triangle.h" +#include "../Shapes/Rhombus.h" +#include "../Shapes/Square.h" +#include + +class ShapesCollection +{ + std::vector collection; + + void free(); + void copyFrom(const ShapesCollection& other); + +public: + + //factory + + void addCircle(double x, double y, double radius); + void addRectangle(double x1, double y1, double x3, double y3); + void addTriangle(double x1, double y1, double x2, double y2, double x3, double y3); + void addRhombus(double x1, double y1, double x2, double y2); + void addSquare(double x1, double y2, double sideLength); + + void printAreas(); + void printPers(); + + ShapesCollection() = default; + ShapesCollection(const ShapesCollection& other); + ShapesCollection& operator=(const ShapesCollection& other); + + ~ShapesCollection(); +}; \ No newline at end of file diff --git a/Sem. 14/Shapes/Circle.cpp b/Sem. 14/Shapes/Circle.cpp new file mode 100644 index 0000000..3f4ac98 --- /dev/null +++ b/Sem. 14/Shapes/Circle.cpp @@ -0,0 +1,22 @@ +#include "Circle.h" + +const double PI = 3.1415; +Circle::Circle(double x, double y, double radius) : Shape(1), radius(radius) +{ + setPoint(0, x, y); +} + +double Circle::getArea() const +{ + return PI * radius * radius; +} +double Circle::getPer() const +{ + return 2 * PI * radius; +} + +Shape* Circle::clone() const +{ + Circle* copy = new Circle(*this); + return copy; +} \ No newline at end of file diff --git a/Sem. 14/Shapes/Circle.h b/Sem. 14/Shapes/Circle.h new file mode 100644 index 0000000..84380fa --- /dev/null +++ b/Sem. 14/Shapes/Circle.h @@ -0,0 +1,15 @@ +#pragma once +#include "Shape.h" + +class Circle : public Shape +{ + double radius; + +public: + Circle(double x, double y, double radius); + + double getArea() const override; + double getPer() const override; + + Shape* clone() const override; +}; \ No newline at end of file diff --git a/Sem. 14/Shapes/Rectangle.cpp b/Sem. 14/Shapes/Rectangle.cpp new file mode 100644 index 0000000..9e16b01 --- /dev/null +++ b/Sem. 14/Shapes/Rectangle.cpp @@ -0,0 +1,32 @@ +#include "Rectangle.h" + +Rectangle::Rectangle(double x1, double y1, double x3, double y3) : Shape(4) +{ + setPoint(0, x1, y1); + setPoint(1, x1, y3); + setPoint(2, x3, y3); + setPoint(3, x3, y1); +} +double Rectangle::getArea() const +{ + Shape::point p0 = getPointAtIndex(0); + Shape::point p1 = getPointAtIndex(1); + Shape::point p3 = getPointAtIndex(3); + + return p0.getDist(p1) * p0.getDist(p3); + +} +double Rectangle::getPer() const +{ + Shape::point p0 = getPointAtIndex(0); + Shape::point p1 = getPointAtIndex(1); + Shape::point p3 = getPointAtIndex(3); + + return 2*(p0.getDist(p1) + p0.getDist(p3)); +} + +Shape* Rectangle::clone() const +{ + Rectangle* copy = new Rectangle(*this); + return copy; +} \ No newline at end of file diff --git a/Sem. 14/Shapes/Rectangle.h b/Sem. 14/Shapes/Rectangle.h new file mode 100644 index 0000000..baa4cb5 --- /dev/null +++ b/Sem. 14/Shapes/Rectangle.h @@ -0,0 +1,13 @@ +#pragma once +#include "Shape.h" + +class Rectangle : virtual public Shape +{ + +public: + Rectangle(double x1, double y1, double x3, double y3); + double getArea() const override; + double getPer() const override; + + Shape* clone() const override; +}; \ No newline at end of file diff --git a/Sem. 14/Shapes/Rhombus.cpp b/Sem. 14/Shapes/Rhombus.cpp new file mode 100644 index 0000000..62a3e8c --- /dev/null +++ b/Sem. 14/Shapes/Rhombus.cpp @@ -0,0 +1,25 @@ +#include "Rhombus.h" + +Rhombus::Rhombus(double x1, double y1, double x2, double y2) : Shape(4) +{ + setPoint(0, x1, y1); + setPoint(1, x2, y2); + double sideLength = getPointAtIndex(0).getDist(getPointAtIndex(1)); + + setPoint(2, x2 + sideLength, y2); + setPoint(3, x1 + sideLength, y1); +} + +double Rhombus::getArea() const +{ + return 0.5 * getPointAtIndex(0).getDist(getPointAtIndex(2)) * getPointAtIndex(1).getDist(getPointAtIndex(3)); +} +double Rhombus::getPer() const +{ + return 4 * getPointAtIndex(0).getDist(getPointAtIndex(1)); +} +Shape* Rhombus::clone() const +{ + Rhombus* copy = new Rhombus(*this); + return copy; +} \ No newline at end of file diff --git a/Sem. 14/Shapes/Rhombus.h b/Sem. 14/Shapes/Rhombus.h new file mode 100644 index 0000000..24e5a5b --- /dev/null +++ b/Sem. 14/Shapes/Rhombus.h @@ -0,0 +1,12 @@ +#pragma once +#include "Shape.h" + +class Rhombus : virtual public Shape +{ +public: + Rhombus(double x1, double y1, double x2, double y2); + + double getArea() const override; + double getPer() const override; + Shape* clone() const override; +}; \ No newline at end of file diff --git a/Sem. 14/Shapes/Shape.cpp b/Sem. 14/Shapes/Shape.cpp new file mode 100644 index 0000000..7bc24ef --- /dev/null +++ b/Sem. 14/Shapes/Shape.cpp @@ -0,0 +1,52 @@ +#include "Shape.h" + +Shape::Shape(size_t pointsCount) : pointsCount(pointsCount) +{ + points = new point[pointsCount]; // [0,0], [0,0].... +} + +void Shape::copyFrom(const Shape& other) +{ + points = new point[other.pointsCount]; + + for (int i = 0; i < other.pointsCount; i++) + points[i] = other.points[i]; +} +void Shape::free() +{ + delete[] points; +} + +Shape::Shape(const Shape& other) +{ + copyFrom(other); +} +Shape& Shape::operator= (const Shape& other) +{ + if (this != &other) + { + free(); + copyFrom(other); + } + return *this; +} +Shape::~Shape() +{ + free(); +} + +const Shape::point& Shape::getPointAtIndex(size_t index) const +{ + if (index >= pointsCount) + throw std::exception("Invalid point index!"); + + return points[index]; +} + +void Shape::setPoint(size_t pointIndex, double x, double y) +{ + if (pointIndex >= pointsCount) + throw std::exception("Invalid point index!"); + + points[pointIndex] = point(x, y); +} diff --git a/Sem. 14/Shapes/Shape.h b/Sem. 14/Shapes/Shape.h new file mode 100644 index 0000000..caf7919 --- /dev/null +++ b/Sem. 14/Shapes/Shape.h @@ -0,0 +1,43 @@ +#pragma once +#include + +class Shape // Abstract class - no instances of Shape are allowed! +{ +protected: + struct point + { + point() :x(0), y(0){} + point(double x, double y) :x(x), y(y){} + double x; + double y; + double getDist(const point& other) const + { + int dx = x - other.x; + int dy = y - other.y; + + return sqrt(dx*dx + dy*dy); + } + }; + const point& getPointAtIndex(size_t index) const; +private: + point* points; + size_t pointsCount; + + void copyFrom(const Shape& other); + void free(); + + +public: + Shape(size_t pointsCount); + + Shape(const Shape& other); + Shape& operator=(const Shape& other); + virtual ~Shape(); //!!!!!! + + void setPoint(size_t pointIndex, double x, double y); + + virtual double getArea() const = 0; //pure virtual + virtual double getPer() const = 0; // pure virtual + + virtual Shape* clone() const = 0; // +}; \ No newline at end of file diff --git a/Sem. 14/Shapes/Square.cpp b/Sem. 14/Shapes/Square.cpp new file mode 100644 index 0000000..555c340 --- /dev/null +++ b/Sem. 14/Shapes/Square.cpp @@ -0,0 +1,19 @@ +#include "Square.h" + +Square::Square(double x, double y, double sideLength) : Rectangle(x, y, x + sideLength, y - sideLength), Rhombus(x, y, x, y - sideLength), Shape(4) +{} + +double Square::getArea() const +{ + return Rhombus::getArea(); +} +double Square::getPer() const +{ + return Rectangle::getPer(); +}; + +Shape* Square::clone() const +{ + Square* copy = new Square(*this); + return copy; +} \ No newline at end of file diff --git a/Sem. 14/Shapes/Square.h b/Sem. 14/Shapes/Square.h new file mode 100644 index 0000000..fd78bd8 --- /dev/null +++ b/Sem. 14/Shapes/Square.h @@ -0,0 +1,13 @@ +#include "Rhombus.h" +#include "Rectangle.h" + +class Square : public Rhombus, public Rectangle +{ +public: + Square(double x, double y, double sideLength); + + double getArea() const override; + double getPer() const override; + + Shape* clone() const override; +}; \ No newline at end of file diff --git a/Sem. 14/Shapes/Triangle.cpp b/Sem. 14/Shapes/Triangle.cpp new file mode 100644 index 0000000..e13d685 --- /dev/null +++ b/Sem. 14/Shapes/Triangle.cpp @@ -0,0 +1,30 @@ +#include "Triangle.h" + +Triangle::Triangle(double x1, double y1, double x2, double y2, double x3, double y3) : Shape(3) +{ + setPoint(0, x1, y1); + setPoint(1, x2, y2); + setPoint(2, x3, y3); +} +double Triangle::getArea() const +{ + Shape::point p1 = getPointAtIndex(0); + Shape::point p2 = getPointAtIndex(1); + Shape::point p3 = getPointAtIndex(2); + + return abs(p1.x*p2.y + p2.x*p3.y + p3.x*p1.y - p1.y * p2.x - p2.y*p3.x - p3.y*p1.x) / 2.00; +} +double Triangle::getPer() const +{ + Shape::point p1 = getPointAtIndex(0); + Shape::point p2 = getPointAtIndex(1); + Shape::point p3 = getPointAtIndex(2); + + return p1.getDist(p2) + p2.getDist(p3) + p3.getDist(p1); +} + +Shape* Triangle::clone() const +{ + Triangle* copy = new Triangle(*this); + return copy; +} \ No newline at end of file diff --git a/Sem. 14/Shapes/Triangle.h b/Sem. 14/Shapes/Triangle.h new file mode 100644 index 0000000..3a53901 --- /dev/null +++ b/Sem. 14/Shapes/Triangle.h @@ -0,0 +1,14 @@ +#pragma once + +#include "Shape.h" + +class Triangle : public Shape +{ + +public: + Triangle(double x1, double y1, double x2, double y2, double x3, double y3); + double getArea() const override; + double getPer() const override; + + Shape* clone() const override; +}; \ No newline at end of file diff --git a/Sem. 14/ShapesCollection/Circle.cpp b/Sem. 14/ShapesCollection/Circle.cpp deleted file mode 100644 index 3f4ac98..0000000 --- a/Sem. 14/ShapesCollection/Circle.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "Circle.h" - -const double PI = 3.1415; -Circle::Circle(double x, double y, double radius) : Shape(1), radius(radius) -{ - setPoint(0, x, y); -} - -double Circle::getArea() const -{ - return PI * radius * radius; -} -double Circle::getPer() const -{ - return 2 * PI * radius; -} - -Shape* Circle::clone() const -{ - Circle* copy = new Circle(*this); - return copy; -} \ No newline at end of file diff --git a/Sem. 14/ShapesCollection/Circle.h b/Sem. 14/ShapesCollection/Circle.h deleted file mode 100644 index 84380fa..0000000 --- a/Sem. 14/ShapesCollection/Circle.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once -#include "Shape.h" - -class Circle : public Shape -{ - double radius; - -public: - Circle(double x, double y, double radius); - - double getArea() const override; - double getPer() const override; - - Shape* clone() const override; -}; \ No newline at end of file diff --git a/Sem. 14/ShapesCollection/Rectangle.cpp b/Sem. 14/ShapesCollection/Rectangle.cpp deleted file mode 100644 index 9e16b01..0000000 --- a/Sem. 14/ShapesCollection/Rectangle.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "Rectangle.h" - -Rectangle::Rectangle(double x1, double y1, double x3, double y3) : Shape(4) -{ - setPoint(0, x1, y1); - setPoint(1, x1, y3); - setPoint(2, x3, y3); - setPoint(3, x3, y1); -} -double Rectangle::getArea() const -{ - Shape::point p0 = getPointAtIndex(0); - Shape::point p1 = getPointAtIndex(1); - Shape::point p3 = getPointAtIndex(3); - - return p0.getDist(p1) * p0.getDist(p3); - -} -double Rectangle::getPer() const -{ - Shape::point p0 = getPointAtIndex(0); - Shape::point p1 = getPointAtIndex(1); - Shape::point p3 = getPointAtIndex(3); - - return 2*(p0.getDist(p1) + p0.getDist(p3)); -} - -Shape* Rectangle::clone() const -{ - Rectangle* copy = new Rectangle(*this); - return copy; -} \ No newline at end of file diff --git a/Sem. 14/ShapesCollection/Rectangle.h b/Sem. 14/ShapesCollection/Rectangle.h deleted file mode 100644 index baa4cb5..0000000 --- a/Sem. 14/ShapesCollection/Rectangle.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once -#include "Shape.h" - -class Rectangle : virtual public Shape -{ - -public: - Rectangle(double x1, double y1, double x3, double y3); - double getArea() const override; - double getPer() const override; - - Shape* clone() const override; -}; \ No newline at end of file diff --git a/Sem. 14/ShapesCollection/Rhombus.cpp b/Sem. 14/ShapesCollection/Rhombus.cpp deleted file mode 100644 index 62a3e8c..0000000 --- a/Sem. 14/ShapesCollection/Rhombus.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "Rhombus.h" - -Rhombus::Rhombus(double x1, double y1, double x2, double y2) : Shape(4) -{ - setPoint(0, x1, y1); - setPoint(1, x2, y2); - double sideLength = getPointAtIndex(0).getDist(getPointAtIndex(1)); - - setPoint(2, x2 + sideLength, y2); - setPoint(3, x1 + sideLength, y1); -} - -double Rhombus::getArea() const -{ - return 0.5 * getPointAtIndex(0).getDist(getPointAtIndex(2)) * getPointAtIndex(1).getDist(getPointAtIndex(3)); -} -double Rhombus::getPer() const -{ - return 4 * getPointAtIndex(0).getDist(getPointAtIndex(1)); -} -Shape* Rhombus::clone() const -{ - Rhombus* copy = new Rhombus(*this); - return copy; -} \ No newline at end of file diff --git a/Sem. 14/ShapesCollection/Rhombus.h b/Sem. 14/ShapesCollection/Rhombus.h deleted file mode 100644 index 24e5a5b..0000000 --- a/Sem. 14/ShapesCollection/Rhombus.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once -#include "Shape.h" - -class Rhombus : virtual public Shape -{ -public: - Rhombus(double x1, double y1, double x2, double y2); - - double getArea() const override; - double getPer() const override; - Shape* clone() const override; -}; \ No newline at end of file diff --git a/Sem. 14/ShapesCollection/Shape.cpp b/Sem. 14/ShapesCollection/Shape.cpp deleted file mode 100644 index 7bc24ef..0000000 --- a/Sem. 14/ShapesCollection/Shape.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include "Shape.h" - -Shape::Shape(size_t pointsCount) : pointsCount(pointsCount) -{ - points = new point[pointsCount]; // [0,0], [0,0].... -} - -void Shape::copyFrom(const Shape& other) -{ - points = new point[other.pointsCount]; - - for (int i = 0; i < other.pointsCount; i++) - points[i] = other.points[i]; -} -void Shape::free() -{ - delete[] points; -} - -Shape::Shape(const Shape& other) -{ - copyFrom(other); -} -Shape& Shape::operator= (const Shape& other) -{ - if (this != &other) - { - free(); - copyFrom(other); - } - return *this; -} -Shape::~Shape() -{ - free(); -} - -const Shape::point& Shape::getPointAtIndex(size_t index) const -{ - if (index >= pointsCount) - throw std::exception("Invalid point index!"); - - return points[index]; -} - -void Shape::setPoint(size_t pointIndex, double x, double y) -{ - if (pointIndex >= pointsCount) - throw std::exception("Invalid point index!"); - - points[pointIndex] = point(x, y); -} diff --git a/Sem. 14/ShapesCollection/Shape.h b/Sem. 14/ShapesCollection/Shape.h deleted file mode 100644 index caf7919..0000000 --- a/Sem. 14/ShapesCollection/Shape.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once -#include - -class Shape // Abstract class - no instances of Shape are allowed! -{ -protected: - struct point - { - point() :x(0), y(0){} - point(double x, double y) :x(x), y(y){} - double x; - double y; - double getDist(const point& other) const - { - int dx = x - other.x; - int dy = y - other.y; - - return sqrt(dx*dx + dy*dy); - } - }; - const point& getPointAtIndex(size_t index) const; -private: - point* points; - size_t pointsCount; - - void copyFrom(const Shape& other); - void free(); - - -public: - Shape(size_t pointsCount); - - Shape(const Shape& other); - Shape& operator=(const Shape& other); - virtual ~Shape(); //!!!!!! - - void setPoint(size_t pointIndex, double x, double y); - - virtual double getArea() const = 0; //pure virtual - virtual double getPer() const = 0; // pure virtual - - virtual Shape* clone() const = 0; // -}; \ No newline at end of file diff --git a/Sem. 14/ShapesCollection/ShapesCollection.cpp b/Sem. 14/ShapesCollection/ShapesCollection.cpp deleted file mode 100644 index 0fe828c..0000000 --- a/Sem. 14/ShapesCollection/ShapesCollection.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include "ShapesCollection.h" - -void ShapesCollection::addCircle(double x, double y, double radius) -{ - Circle* ptr = new Circle(x, y, radius); - collection.push_back(ptr); -} -void ShapesCollection::addRectangle(double x1, double y1, double x3, double y3) -{ - Rectangle* ptr = new Rectangle(x1, y1, x3, y3); - collection.push_back(ptr); -} -void ShapesCollection::addTriangle(double x1, double y1, double x2, double y2, double x3, double y3) -{ - Triangle* ptr = new Triangle(x1, y1, x2, y2, x3, y3); - collection.push_back(ptr); -} -void ShapesCollection::addRhombus(double x1, double y1, double x2, double y2) -{ - collection.push_back(new Rhombus(x1, y1, x2, y2)); -} - -void ShapesCollection::addSquare(double x1, double y1, double sideLength) -{ - collection.push_back(new Square(x1, y1, sideLength)); -} - -void ShapesCollection::printAreas() -{ - for (int i = 0; i < collection.size(); i++) - std::cout << collection[i]->getArea() << " " << std::endl; -} -void ShapesCollection::printPers() -{ - for (int i = 0; i < collection.size(); i++) - std::cout << collection[i]->getPer() << " " << std::endl; -} - -void ShapesCollection::copyFrom(const ShapesCollection& other) -{ - for (int i = 0; i < other.collection.size(); i++) - collection.push_back(other.collection[i]->clone()); -} -void ShapesCollection::free() -{ - for (int i = 0; i < collection.size(); i++) - delete collection[i]; - collection.clear(); -} - -ShapesCollection::ShapesCollection(const ShapesCollection& other) -{ - copyFrom(other); -} -ShapesCollection& ShapesCollection::operator= (const ShapesCollection& other) -{ - if (this != &other) - { - free(); - copyFrom(other); - } - return *this; -} -ShapesCollection::~ShapesCollection() -{ - free(); -} \ No newline at end of file diff --git a/Sem. 14/ShapesCollection/ShapesCollection.h b/Sem. 14/ShapesCollection/ShapesCollection.h deleted file mode 100644 index 9077c74..0000000 --- a/Sem. 14/ShapesCollection/ShapesCollection.h +++ /dev/null @@ -1,33 +0,0 @@ -#include "Circle.h" -#include "Rectangle.h" -#include "Triangle.h" -#include "Rhombus.h" -#include "Square.h" -#include - -class ShapesCollection -{ - std::vector collection; - - void free(); - void copyFrom(const ShapesCollection& other); - -public: - - //factory - - void addCircle(double x, double y, double radius); - void addRectangle(double x1, double y1, double x3, double y3); - void addTriangle(double x1, double y1, double x2, double y2, double x3, double y3); - void addRhombus(double x1, double y1, double x2, double y2); - void addSquare(double x1, double y2, double sideLength); - - void printAreas(); - void printPers(); - - ShapesCollection() = default; - ShapesCollection(const ShapesCollection& other); - ShapesCollection& operator=(const ShapesCollection& other); - - ~ShapesCollection(); -}; \ No newline at end of file diff --git a/Sem. 14/ShapesCollection/Square.cpp b/Sem. 14/ShapesCollection/Square.cpp deleted file mode 100644 index 555c340..0000000 --- a/Sem. 14/ShapesCollection/Square.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "Square.h" - -Square::Square(double x, double y, double sideLength) : Rectangle(x, y, x + sideLength, y - sideLength), Rhombus(x, y, x, y - sideLength), Shape(4) -{} - -double Square::getArea() const -{ - return Rhombus::getArea(); -} -double Square::getPer() const -{ - return Rectangle::getPer(); -}; - -Shape* Square::clone() const -{ - Square* copy = new Square(*this); - return copy; -} \ No newline at end of file diff --git a/Sem. 14/ShapesCollection/Square.h b/Sem. 14/ShapesCollection/Square.h deleted file mode 100644 index fd78bd8..0000000 --- a/Sem. 14/ShapesCollection/Square.h +++ /dev/null @@ -1,13 +0,0 @@ -#include "Rhombus.h" -#include "Rectangle.h" - -class Square : public Rhombus, public Rectangle -{ -public: - Square(double x, double y, double sideLength); - - double getArea() const override; - double getPer() const override; - - Shape* clone() const override; -}; \ No newline at end of file diff --git a/Sem. 14/ShapesCollection/Triangle.cpp b/Sem. 14/ShapesCollection/Triangle.cpp deleted file mode 100644 index e13d685..0000000 --- a/Sem. 14/ShapesCollection/Triangle.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include "Triangle.h" - -Triangle::Triangle(double x1, double y1, double x2, double y2, double x3, double y3) : Shape(3) -{ - setPoint(0, x1, y1); - setPoint(1, x2, y2); - setPoint(2, x3, y3); -} -double Triangle::getArea() const -{ - Shape::point p1 = getPointAtIndex(0); - Shape::point p2 = getPointAtIndex(1); - Shape::point p3 = getPointAtIndex(2); - - return abs(p1.x*p2.y + p2.x*p3.y + p3.x*p1.y - p1.y * p2.x - p2.y*p3.x - p3.y*p1.x) / 2.00; -} -double Triangle::getPer() const -{ - Shape::point p1 = getPointAtIndex(0); - Shape::point p2 = getPointAtIndex(1); - Shape::point p3 = getPointAtIndex(2); - - return p1.getDist(p2) + p2.getDist(p3) + p3.getDist(p1); -} - -Shape* Triangle::clone() const -{ - Triangle* copy = new Triangle(*this); - return copy; -} \ No newline at end of file diff --git a/Sem. 14/ShapesCollection/Triangle.h b/Sem. 14/ShapesCollection/Triangle.h deleted file mode 100644 index 3a53901..0000000 --- a/Sem. 14/ShapesCollection/Triangle.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include "Shape.h" - -class Triangle : public Shape -{ - -public: - Triangle(double x1, double y1, double x2, double y2, double x3, double y3); - double getArea() const override; - double getPer() const override; - - Shape* clone() const override; -}; \ No newline at end of file diff --git a/Sem. 14/ShapesCollection/main.cpp b/Sem. 14/ShapesCollection/main.cpp deleted file mode 100644 index 07202b2..0000000 --- a/Sem. 14/ShapesCollection/main.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include "ShapesCollection.h" -using namespace std; - - -//Ромб -int main() -{ - ShapesCollection s; - - s.addCircle(3, 4, 5); - s.addTriangle(2, 3, 54, 3, 2, 1); - s.addRectangle(1, 2, 3, 4); - s.addRhombus(3, 2, 0, 0); - s.addSquare(4, 4, 10); - - s.printAreas(); -} diff --git a/Sem. 14/main.cpp b/Sem. 14/main.cpp new file mode 100644 index 0000000..6604d49 --- /dev/null +++ b/Sem. 14/main.cpp @@ -0,0 +1,18 @@ +#include +#include "Collection/ShapesCollection.h" +using namespace std; + + +//Ромб +int main() +{ + ShapesCollection s; + + s.addCircle(3, 4, 5); + s.addTriangle(2, 3, 54, 3, 2, 1); + s.addRectangle(1, 2, 3, 4); + s.addRhombus(3, 2, 0, 0); + s.addSquare(4, 4, 10); + + s.printAreas(); +} -- cgit v1.3.1-23-gd82d