From eda2b5dee821c474644cd72df4d0bd93b0940ec6 Mon Sep 17 00:00:00 2001 From: angeld55 Date: Tue, 17 May 2022 20:15:26 +0300 Subject: sem 12 --- Sem. 12/Example_polymorphism/Circle.cpp | 16 +++++++++ Sem. 12/Example_polymorphism/Circle.h | 14 ++++++++ Sem. 12/Example_polymorphism/Rectangle.cpp | 26 +++++++++++++++ Sem. 12/Example_polymorphism/Rectangle.h | 10 ++++++ Sem. 12/Example_polymorphism/Shape.cpp | 52 ++++++++++++++++++++++++++++++ Sem. 12/Example_polymorphism/Shape.h | 41 +++++++++++++++++++++++ Sem. 12/Example_polymorphism/Triangle.cpp | 24 ++++++++++++++ Sem. 12/Example_polymorphism/Triangle.h | 12 +++++++ Sem. 12/Example_polymorphism/main.cpp | 41 +++++++++++++++++++++++ 9 files changed, 236 insertions(+) create mode 100644 Sem. 12/Example_polymorphism/Circle.cpp create mode 100644 Sem. 12/Example_polymorphism/Circle.h create mode 100644 Sem. 12/Example_polymorphism/Rectangle.cpp create mode 100644 Sem. 12/Example_polymorphism/Rectangle.h create mode 100644 Sem. 12/Example_polymorphism/Shape.cpp create mode 100644 Sem. 12/Example_polymorphism/Shape.h create mode 100644 Sem. 12/Example_polymorphism/Triangle.cpp create mode 100644 Sem. 12/Example_polymorphism/Triangle.h create mode 100644 Sem. 12/Example_polymorphism/main.cpp (limited to 'Sem. 12/Example_polymorphism') diff --git a/Sem. 12/Example_polymorphism/Circle.cpp b/Sem. 12/Example_polymorphism/Circle.cpp new file mode 100644 index 0000000..8226582 --- /dev/null +++ b/Sem. 12/Example_polymorphism/Circle.cpp @@ -0,0 +1,16 @@ +#include "Circle.h" + +const double PI = 3.1415; +Circle::Circle(int x, int 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; +} \ No newline at end of file diff --git a/Sem. 12/Example_polymorphism/Circle.h b/Sem. 12/Example_polymorphism/Circle.h new file mode 100644 index 0000000..6449fc1 --- /dev/null +++ b/Sem. 12/Example_polymorphism/Circle.h @@ -0,0 +1,14 @@ +#pragma once +#include "Shape.h" + +class Circle : public Shape +{ + double radius; + +public: + Circle(int x, int y, double radius); + + double getArea() const override; + double getPer() const override; + +}; \ No newline at end of file diff --git a/Sem. 12/Example_polymorphism/Rectangle.cpp b/Sem. 12/Example_polymorphism/Rectangle.cpp new file mode 100644 index 0000000..3fa1026 --- /dev/null +++ b/Sem. 12/Example_polymorphism/Rectangle.cpp @@ -0,0 +1,26 @@ +#include "Rectangle.h" + +Rectangle::Rectangle(int x1, int y1, int x3, int 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)); +} \ No newline at end of file diff --git a/Sem. 12/Example_polymorphism/Rectangle.h b/Sem. 12/Example_polymorphism/Rectangle.h new file mode 100644 index 0000000..ccdad8e --- /dev/null +++ b/Sem. 12/Example_polymorphism/Rectangle.h @@ -0,0 +1,10 @@ +#pragma once +#include "Shape.h" + +class Rectangle : public Shape +{ +public: + Rectangle(int x1, int y1, int x3, int y3); + double getArea() const override; + double getPer() const override; +}; \ No newline at end of file diff --git a/Sem. 12/Example_polymorphism/Shape.cpp b/Sem. 12/Example_polymorphism/Shape.cpp new file mode 100644 index 0000000..27bb9e9 --- /dev/null +++ b/Sem. 12/Example_polymorphism/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, int x, int y) +{ + if (pointIndex >= pointsCount) + throw std::exception("Invalid point index!"); + + points[pointIndex] = point(x, y); +} diff --git a/Sem. 12/Example_polymorphism/Shape.h b/Sem. 12/Example_polymorphism/Shape.h new file mode 100644 index 0000000..62f5c41 --- /dev/null +++ b/Sem. 12/Example_polymorphism/Shape.h @@ -0,0 +1,41 @@ +#pragma once +#include + +class Shape // Abstract class - no instances of Shape are allowed! +{ +protected: + struct point + { + point() :x(0), y(0){} + point(int x, int y) :x(x), y(y){} + int x; + int 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, int x, int y); + + virtual double getArea() const = 0; //pure virtual + virtual double getPer() const = 0; // pure virtual +}; \ No newline at end of file diff --git a/Sem. 12/Example_polymorphism/Triangle.cpp b/Sem. 12/Example_polymorphism/Triangle.cpp new file mode 100644 index 0000000..7de831c --- /dev/null +++ b/Sem. 12/Example_polymorphism/Triangle.cpp @@ -0,0 +1,24 @@ +#include "Triangle.h" + +Triangle::Triangle(int x1, int y1, int x2, int y2, int x3, int 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); +} \ No newline at end of file diff --git a/Sem. 12/Example_polymorphism/Triangle.h b/Sem. 12/Example_polymorphism/Triangle.h new file mode 100644 index 0000000..c157de8 --- /dev/null +++ b/Sem. 12/Example_polymorphism/Triangle.h @@ -0,0 +1,12 @@ +#pragma once + +#include "Shape.h" + +class Triangle : public Shape +{ + +public: + Triangle(int x1, int y1, int x2, int y2, int x3, int y3); + double getArea() const override; + double getPer() const override; +}; \ No newline at end of file diff --git a/Sem. 12/Example_polymorphism/main.cpp b/Sem. 12/Example_polymorphism/main.cpp new file mode 100644 index 0000000..89a5082 --- /dev/null +++ b/Sem. 12/Example_polymorphism/main.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; + +#include "Rectangle.h" +#include "Circle.h" +#include "Triangle.h" + + +void printAreas(const Shape* const * shapes, size_t shapesCount) +{ + for (int i = 0; i < shapesCount; i++) + cout << shapes[i]->getArea() <getPer() << endl; +} + +void freeCollection(Shape** shapes, size_t shapesCount) +{ + for (int i = 0; i < shapesCount; i++) + delete shapes[i]; + delete[] shapes; +} +int main() +{ + Shape** arr = new Shape*[4]; + + arr[0] = new Rectangle(3, 4, 6, 8); + arr[1] = new Circle(3, 3, 4); + arr[2] = new Circle(1, 4, 5); + arr[3] = new Triangle(1, 1, 2, 2, 3, 3); + + printAreas(arr, 4); + printPers(arr, 4); + + freeCollection(arr, 4); + + +} \ No newline at end of file -- cgit v1.3.1-23-gd82d