Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 12
diff options
context:
space:
mode:
Commit8cf1283a4ffb9fe1901c29c087fa376de82726af (patch)
Author
 angeld55 <angel_dimitriev@abv.bg>
2022-02-19 23:30:52 +0200
Committer
 angeld55 <angel_dimitriev@abv.bg>
2022-02-19 23:30:52 +0200
Filesabdfe6bf526100dcce87a88563100e02adf7605e /Sem. 12
Object-oriented_programming_FMI-8cf1283a4ffb9fe1901c29c087fa376de82726af.tar
Object-oriented_programming_FMI-8cf1283a4ffb9fe1901c29c087fa376de82726af.tar.gz
Object-oriented_programming_FMI-8cf1283a4ffb9fe1901c29c087fa376de82726af.zip
Parentd73a7ec225516657dec74472a0491330e9999217 (diff)
clean up
diff --git a/Sem. 12/README.md b/Sem. 12/README.md
deleted file mode 100644
index 5c13eba..0000000
--- a/Sem. 12/README.md
+++ /dev/null
@@ -1,190 +0,0 @@
-
-# Наследяване
-## Пример за наследяване
-
-При създаване на нов клас, който има общи компоненти и поведение с вече дефиниран клас, вместо да дефинира повторно тези компоненти и поведение, можем да го обявим за наследник на вече дефинирания.
-
-```c++
-class A
-{
- .
- .
- .
-public:
- void f()
- {
- .
- .
- .
- }
- .
- .
- .
-};
-class B : public A
-{
- .
- .
- .
-}
-
-int main()
-{
- B obj;
- obj.f(); // OK!
-}
- ```
- При наследяването:
- - Наследяват се член-данните и методите на основните класове
- - Получава се достъп до **някои** от наследените компоненти на основните класове
-
-
-В нашия пример класът **В е наследник на класа А.** Обекти от класовете изглеждат така:
-
-![enter image description here](https://i.ibb.co/Fshm2LJ/Untitled-Diagram.png)
-
-
-След като В е наследник на класа А, то при създаване на обект от тип В се създава обект от тип А, **който е част от обекта** от тип В.
-
-
-## Видове наследяване
-Някои типове наследяване в c++:
-
- - private (по подразбиране)
- - protected
- - public
-
-Примери:
-```c++
-class A
-{
- public:
- int x;
- protected:
- int y;
- private:
- int z;
-};
-
-class B : public A
-{
- // x is public
- // y is protected
- // z is not accessible from B
-};
-
-class C : protected A
-{
- // x is protected
- // y is protected
- // z is not accessible from C
-};
-
-class D : private A // 'private' is default for classes
-{
- // x is private
- // y is private
- // z is not accessible from D
-};
- ```
-
-## Подаване като параметри на функции.
-Класове-наследници могат да бъдат подавани като параметри на функции, които приемат обекти от базовия клас. Може да се използва функционалността само от базовия клас.
-
-```c++
-class A
-{
- int a;
-}
-class B : public A
-{
-public:
- int b;
-}
-void f(A& obj)
-{
- obj.a++;
-}
-
-int main()
-{
- A obj1;
- B obj2;
-
- f(obj1); //OK!
- f(obj2); //OK!
-}
- ```
-
-## Конструктури и деструктори при наследяване.
-Във всеки контструктор на класът-наследник, трябва да се оказва кой конструктор да се извика за базовия клас. Ако не оказано, ще се извика конструктора по подразбиране.
-
-```c++
-class B : public A
-{
-.
-.
-.
-public:
- B(...) : A(...) //some constructor of A.
- {
- .
- .
- .
- }
-}
- ```
-Деструкторът на наследния клас извиква деструктора на базовия клас.
-
-
-## Копиране при наследяване
-При разписване на конструктора за копиране и оператора за присовяване на клас-наследник, трябва експлицитно да извикваме копиране и за базовия клас.
- ```c++
-B::B(const B& other) : A(other) // (copy constructor of A (base class)
-{
- copyFrom(other);
-}
-
-B& B::operator=(const B& other)
-{
-
- if (this != &other)
- {
- A::operator=(other); //operator= of A (base class)
- free();
- copyFrom(other);
- }
- return *this;
-}
- ```
-
-
-
-
-## Задачи
-
-**Задача 1:**
-Реализирайте класове бар, от който ще могат да се взимат напитки (нормални и алкохолни) в определен ред. Редът ще бъде по зареждането им в бара.
-**Напитката** има *име и количество (в мл)*, а **алкохолната напитка** има *име, количество и процент алкохол.*
-
-Примерен интерфейс:
- ```c++
-
-int main()
-{
- Bar b;
-
- b.addDrink("Coke", 330);
-
- b.addDrink("Fanta", 500);
-
- b.addAlocoholDrink("Zagorka", 500, 5);
-
- b.getAcloholDrink(); // Zagorka
-
- b.getDrink() // Coke
- b.getDrink() // Fanta
-}
- ```
-
-
diff --git a/Sem. 12/task1/Bar/Bar.cpp b/Sem. 12/task1/Bar/Bar.cpp
deleted file mode 100644
index 0235801..0000000
--- a/Sem. 12/task1/Bar/Bar.cpp
+++ /dev/null
@@ -1,45 +0,0 @@
-#include "Bar.h"
-
-void Bar::addDrink(const char* name, int ml)
-{
- drinks.enqueue(Drink(name, ml));
-}
-void Bar::addAlocoholDrink(const char* name, int ml, int alcohol)
-{
- alcoholDrinks.enqueue(AlcoholDrink(name, ml, alcohol));
-}
-
-Drink Bar::getDrink()
-{
- Drink d = drinks.dequeue();
- incrementStatistics(d);
- return d;
-}
-AlcoholDrink Bar::getAcloholDrink()
-{
- AlcoholDrink d = alcoholDrinks.dequeue();
- incrementStatistics(d, true);
- return d;
-}
-
-void Bar::incrementStatistics(const Drink& dr, bool isAlcoholDrink)
-{
- stats.mlSold += dr.getMl();
- stats.allDrinksSold++;
-
- if (isAlcoholDrink)
- stats.alcoholDrinksSold++;
-}
-
-size_t Bar::getMlSold() const
-{
- return stats.mlSold;
-}
-size_t Bar::getAllDrinksSold() const
-{
- return stats.allDrinksSold;
-}
-size_t Bar::getAlcoholDrinksSold() const
-{
- return stats.alcoholDrinksSold;
-}
diff --git a/Sem. 12/task1/Bar/Bar.h b/Sem. 12/task1/Bar/Bar.h
deleted file mode 100644
index 06e7d92..0000000
--- a/Sem. 12/task1/Bar/Bar.h
+++ /dev/null
@@ -1,36 +0,0 @@
-#pragma once
-#include <iostream>
-#include "../Drinks/AlcoholDrink.h"
-#include "../Queue/Queue.hpp"
-
-
-
-class Bar
-{
- struct Statistics
- {
- size_t mlSold = 0;
- size_t allDrinksSold = 0;
- size_t alcoholDrinksSold = 0;
- };
-
-private:
- Queue<Drink> drinks;
- Queue<AlcoholDrink> alcoholDrinks;
-
- Statistics stats;
-
- void incrementStatistics(const Drink& dr, bool isAlcoholDrink = false);
-
-public:
-
- void addDrink(const char* name, int ml);
- void addAlocoholDrink(const char* name, int ml, int alcohol);
-
- Drink getDrink();
- AlcoholDrink getAcloholDrink();
-
- size_t getMlSold() const;
- size_t getAllDrinksSold() const;
- size_t getAlcoholDrinksSold() const;
-};
diff --git a/Sem. 12/task1/Drinks/AlcoholDrink.cpp b/Sem. 12/task1/Drinks/AlcoholDrink.cpp
deleted file mode 100644
index 4171ff0..0000000
--- a/Sem. 12/task1/Drinks/AlcoholDrink.cpp
+++ /dev/null
@@ -1,24 +0,0 @@
-#pragma warning(disable: 4996)
-#include "AlcoholDrink.h"
-
-AlcoholDrink::AlcoholDrink() : AlcoholDrink("Unknown", 0, 0){}
-
-AlcoholDrink::AlcoholDrink(const char* name, int ml, int alcoholPercentage) : Drink(name, ml)
-{
- setAlcoholPercentage(alcoholPercentage);
-}
-int AlcoholDrink::getAlcoholPercentage() const
-{
- return alcoholPercentage;
-}
-void AlcoholDrink::setAlcoholPercentage(int alcoholPercentage)
-{
- const int MINPERCENTAGE = 1;
- const int MAXPERCENTAGE = 97;
-
- this->alcoholPercentage = MINPERCENTAGE;
-
- if (MINPERCENTAGE <= alcoholPercentage && alcoholPercentage <= MAXPERCENTAGE)
- this->alcoholPercentage = alcoholPercentage;
-
-} \ No newline at end of file
diff --git a/Sem. 12/task1/Drinks/AlcoholDrink.h b/Sem. 12/task1/Drinks/AlcoholDrink.h
deleted file mode 100644
index 89b2da3..0000000
--- a/Sem. 12/task1/Drinks/AlcoholDrink.h
+++ /dev/null
@@ -1,15 +0,0 @@
-#pragma once
-
-#include "Drink.h"
-
-class AlcoholDrink : public Drink
-{
- int alcoholPercentage;
-
-public:
- AlcoholDrink();
- AlcoholDrink(const char* name, int ml, int alcoholPercentage);
-
- int getAlcoholPercentage() const;
- void setAlcoholPercentage(int alcoholPercentage);
-}; \ No newline at end of file
diff --git a/Sem. 12/task1/Drinks/Drink.cpp b/Sem. 12/task1/Drinks/Drink.cpp
deleted file mode 100644
index 55b3e3a..0000000
--- a/Sem. 12/task1/Drinks/Drink.cpp
+++ /dev/null
@@ -1,69 +0,0 @@
-#pragma warning(disable: 4996)
-#include "Drink.h"
-#include <iostream>
-
-Drink::Drink() : Drink("Unknown", 0)
-{}
-const char* Drink::getName() const
-{
- return name;
-}
-int Drink::getMl() const
-{
- return ml;
-}
-
-void Drink::setName(const char* name)
-{
- delete[] this->name;
-
- this->name = new char[strlen(name) + 1];
- strcpy(this->name, name);
-
-}
-void Drink::setMl(int ml)
-{
- const int MAX_ML = 1000;
- const int MIN_ML = 100;
-
- ml = MIN_ML;
- if (MIN_ML <= ml && ml <= MAX_ML)
- this->ml = ml;
-
-}
-
-Drink::Drink(const char* name, int ml) : name(nullptr)
-{
- setName(name);
- setMl(ml);
-}
-
-Drink::Drink(const Drink& other)
-{
- copyFrom(other);
-}
-Drink& Drink::operator = (const Drink& other)
-{
- if (this != &other)
- {
- free();
- copyFrom(other);
- }
- return *this;
-}
-Drink::~Drink()
-{
- free();
-}
-void Drink::copyFrom(const Drink& other)
-{
- name = new char[strlen(other.name) + 1];
- strcpy(name, other.name);
-
- ml = other.ml;
-}
-void Drink::free()
-{
- delete[] name;
- ml = 0;
-} \ No newline at end of file
diff --git a/Sem. 12/task1/Drinks/Drink.h b/Sem. 12/task1/Drinks/Drink.h
deleted file mode 100644
index d4d37ae..0000000
--- a/Sem. 12/task1/Drinks/Drink.h
+++ /dev/null
@@ -1,27 +0,0 @@
-#pragma once
-
-class Drink
-{
-public:
- Drink();
-
- Drink(const char* name, int ml);
-
- Drink(const Drink& other);
- Drink& operator=(const Drink& other);
- ~Drink();
-
- const char* getName() const;
- int getMl() const;
-
- void setName(const char* name);
- void setMl(int ml);
-
-private:
- char* name;
- int ml;
-
- void copyFrom(const Drink& other);
- void free();
-
-}; \ No newline at end of file
diff --git a/Sem. 12/task1/Drinks/EnergyDrink.cpp b/Sem. 12/task1/Drinks/EnergyDrink.cpp
deleted file mode 100644
index ef61456..0000000
--- a/Sem. 12/task1/Drinks/EnergyDrink.cpp
+++ /dev/null
@@ -1,49 +0,0 @@
-#pragma warning(disable: 4996)
-#include "EnergyDrink.h"
-#include <iostream>
-
-EnergyDrink::EnergyDrink(const char* name, int ml, const char* sertificate) : Drink(name, ml), qualityCertificate(nullptr)
-{
- setCertificate(sertificate);
-}
-const char* EnergyDrink::getCertifiate() const
-{
- return qualityCertificate;
-}
-void EnergyDrink::setCertificate(const char* certficate)
-{
- delete[] qualityCertificate;
-
- qualityCertificate = new char[strlen(certficate) + 1];
- strcpy(qualityCertificate, certficate);
-
-}
-EnergyDrink::EnergyDrink(const EnergyDrink& other) : Drink(other)
-{
- copyFrom(other);
-}
-
-EnergyDrink& EnergyDrink::operator=(const EnergyDrink& other)
-{
- if (this != &other)
- {
- Drink::operator=(other);
- free();
- copyFrom(other);
- }
- return *this;
-}
-
-EnergyDrink::~EnergyDrink()
-{
- free();
-}
-void EnergyDrink::copyFrom(const EnergyDrink& other)
-{
- qualityCertificate = new char[strlen(other.qualityCertificate) + 1];
- strcpy(qualityCertificate, other.qualityCertificate);
-}
-void EnergyDrink::free()
-{
- delete[] qualityCertificate;
-} \ No newline at end of file
diff --git a/Sem. 12/task1/Drinks/EnergyDrink.h b/Sem. 12/task1/Drinks/EnergyDrink.h
deleted file mode 100644
index 99bbbdb..0000000
--- a/Sem. 12/task1/Drinks/EnergyDrink.h
+++ /dev/null
@@ -1,22 +0,0 @@
-// име, мл, сертификат за качество.
-#pragma once
-#include "Drink.h"
-
-class EnergyDrink : public Drink
-{
-private:
- char* qualityCertificate;
-
- void copyFrom(const EnergyDrink& other);
- void free();
-
-public:
- EnergyDrink(const char* name, int ml, const char* certificate);
-
- EnergyDrink(const EnergyDrink& other);
- EnergyDrink& operator=(const EnergyDrink& other);
- ~EnergyDrink();
-
- const char* getCertifiate() const;
- void setCertificate(const char* certificate);
-}; \ No newline at end of file
diff --git a/Sem. 12/task1/Queue/Queue.hpp b/Sem. 12/task1/Queue/Queue.hpp
deleted file mode 100644
index e47460a..0000000
--- a/Sem. 12/task1/Queue/Queue.hpp
+++ /dev/null
@@ -1,145 +0,0 @@
-#pragma once
-#include <iostream>
-
-template <typename T>
-class Queue
-{
- T* data;
- size_t capacity;
- size_t count;
-
- size_t head; //where to get
- size_t tail; //last added element;
-
- void resize(size_t newCapacity);
-
- void copyFrom(const Queue<T>& other);
- void free();
-public:
- Queue();
-
- Queue(const Queue<T>& other);
- Queue<T>& operator=(const Queue<T>& other);
-
- void enqueue(const T& obj);
- T dequeue();
-
- size_t getCount();
- size_t isEmpty();
-
- ~Queue();
-};
-
-template <typename T>
-Queue<T>::Queue()
-{
- capacity = 4;
- data = new T[capacity];
-
- count = 0;
- head = tail = 0;
-}
-
-
-
-template <typename T>
-void Queue<T>::enqueue(const T& obj)
-{
- if (count == capacity)
- resize(capacity * 2);
- if (count != 0)
- tail++;
-
- tail %= capacity; //so we don't get an out of range exception
-
- data[tail] = obj;
-
- count++;
-}
-
-template <typename T>
-T Queue<T>::dequeue()
-{
- if (count == 0)
- throw std::exception("The collection is empty!");
-
- T toReturn = data[head];
- (++head) %= capacity;
-
- count--;
-
- return toReturn;
-}
-
-template <typename T>
-size_t Queue<T>::getCount()
-{
- return count;
-}
-
-template <typename T>
-size_t Queue<T>::isEmpty()
-{
- return getCount() == 0;
-}
-
-template <typename T>
-void Queue<T>::resize(size_t newCapacity)
-{
- T* newData = new T[newCapacity];
-
- for (int i = 0, ind = head; i < count; i++, (++ind) %= capacity)
- newData[i] = data[ind];
-
- delete[] data;
- capacity = newCapacity;
- data = newData;
-
- head = 0;
- tail = count - 1;
-}
-
-template <typename T>
-void Queue<T>::copyFrom(const Queue<T>& other)
-{
- data = new T[other.capacity];
- for (int i = 0; i < other.capacity; i++)
- data[i] = other.data[i];
-
- head = other.head;
- tail = other.tail;
-
- count = other.count;
- capacity = other.capacity;
-
-}
-
-template <typename T>
-void Queue<T>::free()
-{
- delete[] data;
-}
-
-template <typename T>
-Queue<T>::Queue(const Queue<T>& other)
-{
- copyFrom(other);
-
-}
-
-template <typename T>
-Queue<T>& Queue<T>::operator=(const Queue<T>& other)
-{
- if (this != &other)
- {
- free();
- copyFrom(other);
- }
- return *this;
-}
-
-template <typename T>
-Queue<T>::~Queue()
-{
- free();
-}
diff --git a/Sem. 12/task1/main.cpp b/Sem. 12/task1/main.cpp
deleted file mode 100644
index ba367d8..0000000
--- a/Sem. 12/task1/main.cpp
+++ /dev/null
@@ -1,30 +0,0 @@
-#pragma warning(disable: 4996)
-
-
-#include <iostream>
-#include "Bar/Bar.h"
-#include "Drinks/EnergyDrink.h"
-
-using namespace std;
-
-int main()
-{
- Bar b;
-
- b.addDrink("Coke", 330);
-
- b.addDrink("Fanta", 500);
-
- b.addAlocoholDrink("Zagorka", 500, 5);
-
- cout << b.getDrink().getName()<<endl;
- cout << b.getDrink().getName() << endl;
-
- cout << b.getAcloholDrink().getName() << endl;
-
- cout << b.getAllDrinksSold() << endl;
-
- EnergyDrink energyDrink("Monster", 500, "ECAS");
-
- std::cout << energyDrink.getCertifiate();
-}