Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 13
diff options
context:
space:
mode:
10 files changed, 0 insertions, 429 deletions
-rw-r--r--Sem. 13/Figures/Circle.cpp16
-rw-r--r--Sem. 13/Figures/Circle.h14
-rw-r--r--Sem. 13/Figures/Rectangle.cpp26
-rw-r--r--Sem. 13/Figures/Rectangle.h10
-rw-r--r--Sem. 13/Figures/Shape.cpp52
-rw-r--r--Sem. 13/Figures/Shape.h41
-rw-r--r--Sem. 13/Figures/Triangle.cpp24
-rw-r--r--Sem. 13/Figures/Triangle.h12
-rw-r--r--Sem. 13/Figures/main.cpp36
-rw-r--r--Sem. 13/README.md198
diff --git a/Sem. 13/Figures/Circle.cpp b/Sem. 13/Figures/Circle.cpp
deleted file mode 100644
index 8226582..0000000
--- a/Sem. 13/Figures/Circle.cpp
+++ /dev/null
@@ -1,16 +0,0 @@
-#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. 13/Figures/Circle.h b/Sem. 13/Figures/Circle.h
deleted file mode 100644
index 6449fc1..0000000
--- a/Sem. 13/Figures/Circle.h
+++ /dev/null
@@ -1,14 +0,0 @@
-#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. 13/Figures/Rectangle.cpp b/Sem. 13/Figures/Rectangle.cpp
deleted file mode 100644
index 3fa1026..0000000
--- a/Sem. 13/Figures/Rectangle.cpp
+++ /dev/null
@@ -1,26 +0,0 @@
-#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. 13/Figures/Rectangle.h b/Sem. 13/Figures/Rectangle.h
deleted file mode 100644
index ccdad8e..0000000
--- a/Sem. 13/Figures/Rectangle.h
+++ /dev/null
@@ -1,10 +0,0 @@
-#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. 13/Figures/Shape.cpp b/Sem. 13/Figures/Shape.cpp
deleted file mode 100644
index 27bb9e9..0000000
--- a/Sem. 13/Figures/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, int x, int y)
-{
- if (pointIndex >= pointsCount)
- throw std::exception("Invalid point index!");
-
- points[pointIndex] = point(x, y);
-}
diff --git a/Sem. 13/Figures/Shape.h b/Sem. 13/Figures/Shape.h
deleted file mode 100644
index 62f5c41..0000000
--- a/Sem. 13/Figures/Shape.h
+++ /dev/null
@@ -1,41 +0,0 @@
-#pragma once
-#include <iostream>
-
-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. 13/Figures/Triangle.cpp b/Sem. 13/Figures/Triangle.cpp
deleted file mode 100644
index 7de831c..0000000
--- a/Sem. 13/Figures/Triangle.cpp
+++ /dev/null
@@ -1,24 +0,0 @@
-#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. 13/Figures/Triangle.h b/Sem. 13/Figures/Triangle.h
deleted file mode 100644
index c157de8..0000000
--- a/Sem. 13/Figures/Triangle.h
+++ /dev/null
@@ -1,12 +0,0 @@
-#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. 13/Figures/main.cpp b/Sem. 13/Figures/main.cpp
deleted file mode 100644
index 7e9f9ff..0000000
--- a/Sem. 13/Figures/main.cpp
+++ /dev/null
@@ -1,36 +0,0 @@
-#include <iostream>
-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() <<endl;
-}
-void printPers(const Shape* const * shapes, size_t shapesCount)
-{
- for (int i = 0; i < shapesCount; i++)
- cout << shapes[i]->getPer() << endl;
-}
-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);
-
- for (int i = 0; i < 4; i++)
- delete arr[i];
- delete[] arr;
-
-
-} \ No newline at end of file
diff --git a/Sem. 13/README.md b/Sem. 13/README.md
deleted file mode 100644
index 7835c36..0000000
--- a/Sem. 13/README.md
+++ /dev/null
@@ -1,198 +0,0 @@
-# Статично и динамично свързване. Виртуални функции. Полиморфизъм. Абстрактни класове.
-#### Статично и динамично свързване. Виртуални функции.
-Пример за **статично** свързване:
-```c++
-#include <iostream>
-using namespace std;
-
-struct A
-{
- void f() const
- {
- std::cout << "A::f()" << std::endl;
- }
-};
-
-struct B : public A
-{
- void f() const
- {
- std::cout << "B::f()" << std::endl;
- }
-};
-
-int main()
-{
- B* bPtr = new B();
-
- A* aPtr = bPtr;
-
- bPtr->f(); // B::f()
- aPtr->f(); // A::f()
-}
- ```
-
- - **Статично** свързване - изборът на функцията, която трябва да се изпълни става **по време на компилация**.
- - **Динамично** свързване – изборът на функцията, която трябва да се изпълни става **по време на изпълнение на програмата**.
-
-```c++
-#include <iostream>
-using namespace std;
-
-struct A
-{
- virtual void f() const
- {
- std::cout << "A::f()" << std::endl;
- }
-};
-
-struct B : public A
-{
- void f() const
- {
- std::cout << "B::f()" << std::endl;
- }
-};
-
-int main()
-{
- А* ptrs[2];
- ptrs[0] = new A();
- ptrs[1] = new B();
-
- ptrs[0]->f(); // B::f()
- ptrs[1]->f(); // B::f()
-}
- ```
-#### Полиморфизъм
-Едни и същи действия се реализират по различен начин в
-зависимост от обектите, върху които се прилагат.
-
- - Действията се наричат полиморфни.
- - Реализира се чрез виртуални функции.
- - Класовете, върху които ще се прилага, трябва да имат общ родител или прародител, т.е. да са наследници на един и същ клас.
- - В класа се дефинира виртуален метод, съответстващ на полиморфното действие.
- - Всеки клас предефинира или не виртуалния метод.
- - Активирането става чрез указател към базов клас, на който може да се присвоят адресите на обекти на който и да е от базовите класове от йерархията
-
-**Важно!** При полиморфна йерархия ще изтриваме обектите чрез указатели от базовия клас. За да се извикват правилните деструкори задължително **деструкторът на базовият клас** трябва е деклариран като виртуален!
-```c++
-#include<iostream>
-
-struct Animal
-{
- virtual void sayHello() const
- {
- std::cout << "Hello, I am a random animal" << std::endl;
- }
-
- virtual ~Animal(){} //!!!
-};
-struct Dog : public Animal
-{
- void sayHello() const override
- {
- std::cout << "Hello, I am a dog!" << std::endl;
- }
-};
-
-struct Cat : public Animal
-{
- void sayHello() const override
- {
- std::cout << "Hello, I am a cat!" << std::endl;
- }
-};
-
-struct Mouse : public Animal
-{
-};
-
-int main()
-{
- Animal** animals = new Animal*[3];
-
- animals[0] = new Dog();
- animals[1] = new Cat();
- animals[2] = new Mouse();
-
-
- //from base-class pointer
- animals[0]->sayHello(); // Hello, I am a dog!
- animals[1]->sayHello(); // Hello, I am a cat!
- animals[2]->sayHello(); // Hello, I am a random animal!
-
- delete animals[0], animals[1], animals[2];
- delete[] animals;
-}
-
- ```
- #### Абстрактен клас
-
-
- - Чисто виртуална функция (**pure virtual function**) - вруална функция без тяло.
- - Клас е **абстрактен**, ако в него има поне една **чисто виртуална функция**.
-
-Тогава класът е предназначен единствено за наследяване и няма да може да създаваме обекти от него.
-Така във всеки наследник ще трябва да се разпише имплементация на функцията. Ако някой наследник няма собствена имплементация, то и той става абстрактен клас.
-
-```c++
-#include<iostream>
-
-struct Animal
-{
- virtual void sayHello() const = 0;
-
-
- virtual ~Animal(){} //!!!
-};
-struct Dog : public Animal
-{
- void sayHello() const override
- {
- std::cout << "Hello, I am a dog!" << std::endl;
- }
-};
-
-struct Cat : public Animal
-{
- void sayHello() const override
- {
- std::cout << "Hello, I am a cat!" << std::endl;
- }
-};
-
-struct Mouse : public Animal
-{
- void sayHello() const override
- {
- std::cout << "Hello, I am a mouse!" << std::endl;
- }
-};
-
-struct PrehistoricAnimal : public Animal
-{};
-
-int main()
-{
- // Animal* pa1 = new Animal(); No! Animal is an abstract class!
- // Animal* pa2 = new PrehistoricAnimal(); No! PrehistoricAnimal is also an abstract class!
-
- Animal** animals = new Animal*[3];
-
- animals[0] = new Dog();
- animals[1] = new Cat();
- animals[2] = new Mouse();
-
-
- //from base-class pointer
- animals[0]->sayHello(); // Hello, I am a dog!
- animals[1]->sayHello(); // Hello, I am a cat!
- animals[2]->sayHello(); // Hello, I am a random animal!
-
- delete animals[0], animals[1], animals[2];
- delete[] animals;
-}
-
- ```