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/Shape.cpp | 52 ++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Sem. 12/Example_polymorphism/Shape.cpp (limited to 'Sem. 12/Example_polymorphism/Shape.cpp') 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); +} -- cgit v1.3.1-23-gd82d