From 4993c6468aa07e7c6a006a721fe82aaa4413eede Mon Sep 17 00:00:00 2001 From: angeld55 Date: Tue, 17 May 2022 20:42:42 +0300 Subject: added bar task --- Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.cpp | 19 +++ Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.h | 15 ++ Sem. 11/Examples/Bar/Bar/Bar.cpp | 53 ++++++++ Sem. 11/Examples/Bar/Bar/Bar.h | 31 +++++ Sem. 11/Examples/Bar/Drink/Drink.cpp | 62 +++++++++ Sem. 11/Examples/Bar/Drink/Drink.h | 21 +++ Sem. 11/Examples/Bar/MyString/MyString.cpp | 142 +++++++++++++++++++ Sem. 11/Examples/Bar/MyString/MyString.h | 44 ++++++ Sem. 11/Examples/Bar/Pair/Pair.hpp | 43 ++++++ Sem. 11/Examples/Bar/Queue/Queue.hpp | 151 +++++++++++++++++++++ Sem. 11/Examples/Bar/Source2.cpp | 23 ++++ Sem. 11/Examples/Optional+pair.txt | 1 - .../Person, Student, Teacher/Person/Person.cpp | 76 +++++++++++ .../Person, Student, Teacher/Person/Person.h | 27 ++++ .../Examples/Person, Student, Teacher/Source.cpp | 21 +++ .../Person, Student, Teacher/Student/Student.cpp | 34 +++++ .../Person, Student, Teacher/Student/Student.h | 24 ++++ .../Person, Student, Teacher/Teacher/Teacher.cpp | 55 ++++++++ .../Person, Student, Teacher/Teacher/Teacher.h | 16 +++ Sem. 11/Examples/Person/Person.cpp | 78 ----------- Sem. 11/Examples/Person/Person.h | 27 ---- Sem. 11/Examples/Source2.cpp | 10 -- Sem. 11/Examples/Student/Student.cpp | 34 ----- Sem. 11/Examples/Student/Student.h | 25 ---- 24 files changed, 857 insertions(+), 175 deletions(-) create mode 100644 Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.cpp create mode 100644 Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.h create mode 100644 Sem. 11/Examples/Bar/Bar/Bar.cpp create mode 100644 Sem. 11/Examples/Bar/Bar/Bar.h create mode 100644 Sem. 11/Examples/Bar/Drink/Drink.cpp create mode 100644 Sem. 11/Examples/Bar/Drink/Drink.h create mode 100644 Sem. 11/Examples/Bar/MyString/MyString.cpp create mode 100644 Sem. 11/Examples/Bar/MyString/MyString.h create mode 100644 Sem. 11/Examples/Bar/Pair/Pair.hpp create mode 100644 Sem. 11/Examples/Bar/Queue/Queue.hpp create mode 100644 Sem. 11/Examples/Bar/Source2.cpp delete mode 100644 Sem. 11/Examples/Optional+pair.txt create mode 100644 Sem. 11/Examples/Person, Student, Teacher/Person/Person.cpp create mode 100644 Sem. 11/Examples/Person, Student, Teacher/Person/Person.h create mode 100644 Sem. 11/Examples/Person, Student, Teacher/Source.cpp create mode 100644 Sem. 11/Examples/Person, Student, Teacher/Student/Student.cpp create mode 100644 Sem. 11/Examples/Person, Student, Teacher/Student/Student.h create mode 100644 Sem. 11/Examples/Person, Student, Teacher/Teacher/Teacher.cpp create mode 100644 Sem. 11/Examples/Person, Student, Teacher/Teacher/Teacher.h delete mode 100644 Sem. 11/Examples/Person/Person.cpp delete mode 100644 Sem. 11/Examples/Person/Person.h delete mode 100644 Sem. 11/Examples/Source2.cpp delete mode 100644 Sem. 11/Examples/Student/Student.cpp delete mode 100644 Sem. 11/Examples/Student/Student.h (limited to 'Sem. 11/Examples') diff --git a/Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.cpp b/Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.cpp new file mode 100644 index 0000000..ddd890e --- /dev/null +++ b/Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.cpp @@ -0,0 +1,19 @@ +#pragma once +#include "AlcoholDrink.h" + +AlcoholDrink::AlcoholDrink():alcP(5) {} + +AlcoholDrink::AlcoholDrink(const MyString& name, int ml, int alcP) :Drink(name, ml) +{ + setAlcP(alcP); +} +void AlcoholDrink::setAlcP(int alcP) +{ + if (alcP < 5 || alcP > 98) + alcP = 50; + this->alcP = alcP; +} +int AlcoholDrink::getAlcP() const +{ + return alcP; +} \ No newline at end of file diff --git a/Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.h b/Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.h new file mode 100644 index 0000000..3029580 --- /dev/null +++ b/Sem. 11/Examples/Bar/AlcoholDrink/AlcoholDrink.h @@ -0,0 +1,15 @@ +#pragma once +#include "../Drink/Drink.h" +#include "../Queue/Queue.hpp" +#include "../Pair/Pair.hpp" + +class AlcoholDrink : public Drink +{ + int alcP; +public: + AlcoholDrink(); + AlcoholDrink(const MyString& name, int ml, int alcP); + + void setAlcP(int alcP); + int getAlcP() const; +}; \ No newline at end of file diff --git a/Sem. 11/Examples/Bar/Bar/Bar.cpp b/Sem. 11/Examples/Bar/Bar/Bar.cpp new file mode 100644 index 0000000..2d20195 --- /dev/null +++ b/Sem. 11/Examples/Bar/Bar/Bar.cpp @@ -0,0 +1,53 @@ +#include "Bar.h" + +void Bar::addAlcoholDrink(const AlcoholDrink& dr, size_t count) +{ + if (count > 0) + alcoholDrinks.push(Pair(dr, count)); +} +void Bar::addDrink(const Drink& dr, size_t count) +{ + if (count > 0) + drinks.push(Pair(dr, count)); +} + +Drink Bar::getDrink() +{ + + if (drinks.isEmpty()) + throw std::logic_error("Empty drinks collection"); + + Drink toReturn = drinks.peek().getFirst(); + + stats.mlSold += toReturn.getMl(); + stats.soldDrinks++; + + drinks.peek().setSecond(drinks.peek().getSecond() - 1); + if (drinks.peek().getSecond() == 0) + drinks.pop(); + + return toReturn; +} + +AlcoholDrink Bar::getAlcDrink() +{ + if (alcoholDrinks.isEmpty()) + throw std::logic_error("Empty alc. drinks collection"); + + AlcoholDrink toReturn = alcoholDrinks.peek().getFirst(); + + stats.mlSold += toReturn.getMl(); + stats.soldAlcoholDrinks++; + + alcoholDrinks.peek().setSecond(alcoholDrinks.peek().getSecond() - 1); + if (alcoholDrinks.peek().getSecond() == 0) + alcoholDrinks.pop(); + + return toReturn; + +} + +const Bar::Statistics& Bar::getStats() const +{ + return stats; +} \ No newline at end of file diff --git a/Sem. 11/Examples/Bar/Bar/Bar.h b/Sem. 11/Examples/Bar/Bar/Bar.h new file mode 100644 index 0000000..4bf5513 --- /dev/null +++ b/Sem. 11/Examples/Bar/Bar/Bar.h @@ -0,0 +1,31 @@ +#pragma once +#include "../Queue/Queue.hpp" +#include "../Pair/Pair.hpp" +#include "../AlcoholDrink/AlcoholDrink.h" + +class Bar +{ + MyQueue> alcoholDrinks; + MyQueue> drinks; + +public: + struct Statistics + { + size_t soldDrinks; + size_t soldAlcoholDrinks; + size_t mlSold; + } stats; + + + Bar() = default; + + void addAlcoholDrink(const AlcoholDrink& dr, size_t count = 1); + void addDrink(const Drink& dr, size_t count = 1); + + + Drink getDrink(); + AlcoholDrink getAlcDrink(); + + const Statistics& getStats() const; + +}; diff --git a/Sem. 11/Examples/Bar/Drink/Drink.cpp b/Sem. 11/Examples/Bar/Drink/Drink.cpp new file mode 100644 index 0000000..4bb921f --- /dev/null +++ b/Sem. 11/Examples/Bar/Drink/Drink.cpp @@ -0,0 +1,62 @@ +#include "Drink.h" + +Drink::Drink() : Drink("Unknown", 0) +{} + +const MyString& Drink::getName() const +{ + return name; +} +int Drink::getMl() const +{ + return ml; +} + +bool isLower(char ch) +{ + return ch >= 'a' && ch <= 'z'; +} + +bool areAllLowerCase(const char* str) +{ + while (*str) + { + if (!isLower(*str)) + return false; + str++; + } + return true; +} + +bool isUpper(char ch) +{ + return ch >= 'A' && ch <= 'Z'; +} + +bool isNameValid(const char* name) +{ + return isUpper(*name) && areAllLowerCase(name + 1); +} + +void Drink::setName(const MyString& name) +{ + if (isNameValid(name.c_str())) + 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 MyString& name, int ml) +{ + setName(name); + setMl(ml); +} + diff --git a/Sem. 11/Examples/Bar/Drink/Drink.h b/Sem. 11/Examples/Bar/Drink/Drink.h new file mode 100644 index 0000000..d48a725 --- /dev/null +++ b/Sem. 11/Examples/Bar/Drink/Drink.h @@ -0,0 +1,21 @@ +#pragma once +#include "../MyString/MyString.h" + +class Drink +{ +public: + + Drink(); + Drink(const MyString& name, int ml); + + const MyString& getName() const; + int getMl() const; + + void setName(const MyString& name); + void setMl(int ml); + +private: + MyString name; + int ml; + +}; \ No newline at end of file diff --git a/Sem. 11/Examples/Bar/MyString/MyString.cpp b/Sem. 11/Examples/Bar/MyString/MyString.cpp new file mode 100644 index 0000000..3e60278 --- /dev/null +++ b/Sem. 11/Examples/Bar/MyString/MyString.cpp @@ -0,0 +1,142 @@ +#include "MyString.h" +#include +#include + +void MyString::copyFrom(const MyString& other) +{ + str = new char[strlen(other.str) + 1]; + strcpy(str, other.str); + size = other.size; +} +void MyString::free() +{ + delete[] str; +} + +MyString::MyString() +{ + str = new char[1]; + str[0] = '\0'; + size = 0; +} + +MyString::MyString(const char* data) +{ + if(data == nullptr) + { + str = new char[1]; + str[0] = '\0'; + size = 0; + } + else + { + size = strlen(data); + str = new char[size + 1]; + strcpy(str, data); + } +} + +MyString::MyString(const MyString& other) +{ + copyFrom(other); +} +MyString& MyString::operator=(const MyString& other) +{ + if (this != &other) + { + free(); + copyFrom(other); + } + return *this; +} +MyString::~MyString() +{ + free(); +} + + +size_t MyString::getSize() const +{ + return size; +} +void MyString::concat(const MyString& other) +{ + char* temp = new char[getSize() + other.getSize() + 1]; + strcpy(temp, str); + strcat(temp, other.str); + + delete[] str; + str = temp; + size = size + other.getSize(); +} + +const char* MyString::c_str() const +{ + return str; +} + +MyString& MyString::operator+=(const MyString& other) +{ + concat(other); + return *this; +} + +MyString operator+(const MyString& lhs, const MyString& rhs) +{ + MyString copyOfLeft(lhs); + copyOfLeft += rhs; + return copyOfLeft; +} + +std::ostream& operator<<(std::ostream& stream, const MyString& str) +{ + stream << str.str; + return stream; +} + +std::istream& operator>>(std::istream& stream, MyString& str) +{ + delete[] str.str; + char buff[1024]; + stream >> buff; + + str.size = strlen(buff); + str.str = new char[str.size + 1]; + strcpy(str.str, buff); + + return stream; +} + +bool operator==(const MyString& lhs, const MyString& rhs) +{ + return strcmp(lhs.c_str(), rhs.c_str()) == 0; +} +bool operator<=(const MyString& lhs, const MyString& rhs) +{ + return strcmp(lhs.c_str(), rhs.c_str()) <= 0; + +} +bool operator<(const MyString& lhs, const MyString& rhs) +{ + return strcmp(lhs.c_str(), rhs.c_str()) < 0; + +} + +MyString::MyString(MyString&& otherString) +{ + str = otherString.str; + size = otherString.size; + otherString.str = nullptr; +} + +MyString& MyString::operator=(MyString&& otherString) +{ + if (this != &otherString) + { + free(); + str = otherString.str; + otherString.str = nullptr; + size = otherString.size; + } + return *this; +} diff --git a/Sem. 11/Examples/Bar/MyString/MyString.h b/Sem. 11/Examples/Bar/MyString/MyString.h new file mode 100644 index 0000000..b37733a --- /dev/null +++ b/Sem. 11/Examples/Bar/MyString/MyString.h @@ -0,0 +1,44 @@ +#pragma once + +#include +class MyString +{ + char* str; + size_t size; + + void copyFrom(const MyString& other); + void free(); + +public: + MyString(); + MyString(const char*); + + MyString(const MyString&); + + MyString(MyString&&); //move constructor that accepts rvalue; + + MyString& operator=(const MyString& other); + MyString& operator=(MyString&& other); //move op= that accepts rvalue; + + ~MyString(); + + size_t getSize() const; + void concat(const MyString& other); + + const char* c_str() const; + + MyString& operator+=(const MyString& other); + + friend std::ostream& operator<<(std::ostream& stream, const MyString& str); + friend std::istream& operator>>(std::istream& stream, MyString& str); + +}; + +MyString operator+(const MyString& lhs, const MyString& rhs); + +bool operator==(const MyString& lhs, const MyString& rhs); +bool operator<=(const MyString& lhs, const MyString& rhs); +bool operator<(const MyString& lhs, const MyString& rhs); + + +//MyString name = "sdfsdf" \ No newline at end of file diff --git a/Sem. 11/Examples/Bar/Pair/Pair.hpp b/Sem. 11/Examples/Bar/Pair/Pair.hpp new file mode 100644 index 0000000..5f83d0c --- /dev/null +++ b/Sem. 11/Examples/Bar/Pair/Pair.hpp @@ -0,0 +1,43 @@ +#pragma once + +template +class Pair +{ + T first; + mutable D second; // For Drinks task!! +public: + Pair() = default; + Pair(const T& first, const D& second); + const T& getFirst() const; + const D& getSecond() const; + + void setFirst(const T& newValue); + void setSecond(const D& newValue) const ; +}; + +template +Pair::Pair(const T& first, const D& second) : first(first), second(second) +{} + +template +const T& Pair::getFirst() const +{ + return first; +} +template +const D& Pair::getSecond() const +{ + return second; +} + +template +void Pair::setFirst(const T& newValue) +{ + first = newValue; +} + +template +void Pair::setSecond(const D& newValue) const +{ + second = newValue; +} \ No newline at end of file diff --git a/Sem. 11/Examples/Bar/Queue/Queue.hpp b/Sem. 11/Examples/Bar/Queue/Queue.hpp new file mode 100644 index 0000000..5c9af06 --- /dev/null +++ b/Sem. 11/Examples/Bar/Queue/Queue.hpp @@ -0,0 +1,151 @@ +#pragma once +#include +#include +template +class MyQueue +{ + T* data; + size_t capacity; + size_t size; + + size_t get; + size_t put; + + void resize(); + + void copyFrom(const MyQueue& other); + void free(); +public: + MyQueue(); + + MyQueue(const MyQueue& other); + MyQueue& operator=(const MyQueue& other); + + void push(const T& obj); + void push(T&& obj); //позволява и местене + void pop(); + + const T& peek() const; + bool isEmpty() const; + + ~MyQueue(); + +}; + +template +MyQueue::MyQueue() +{ + capacity = 4; + data = new T[capacity]; + size = 0; + get = put = 0; +} + +template +void MyQueue::push(const T& obj) +{ + if (size == capacity) + resize(); + + data[put] = obj; //operator= + (++put) %= capacity; + size++; +} + +template +void MyQueue::push(T&& obj) +{ + if (size == capacity) + resize(); + + data[put] = std::move(obj); //move operator = + (++put) %= capacity; + size++; +} + +template +bool MyQueue::isEmpty() const +{ + return size == 0; +} + + +template +const T& MyQueue::peek() const +{ + if (isEmpty()) + throw std::logic_error("Empty queue!"); + + return data[get]; +} + +template +void MyQueue::pop() +{ + if (isEmpty()) + throw std::logic_error("Empty queue!"); + (++get) %= capacity; + size--; +} + +template +void MyQueue::resize() +{ + T* resizedData = new T[capacity * 2]; + for (size_t i = 0; i < size; i++) + { + resizedData[i] = data[get]; + (++get) %= capacity; + } + capacity *= 2; + delete[] data; + data = resizedData; + get = 0; + put = size; +} + + +template +void MyQueue::copyFrom(const MyQueue& other) +{ + data = new T[other.capacity]; + for (int i = 0; i < other.capacity; i++) + data[i] = other.data[i]; + + get = other.get; + put = other.put; + + size = other.size; + capacity = other.capacity; + +} + +template +void MyQueue::free() +{ + delete[] data; +} + +template +MyQueue::MyQueue(const MyQueue& other) +{ + copyFrom(other); + +} + +template +MyQueue& MyQueue::operator=(const MyQueue& other) +{ + if (this != &other) + { + free(); + copyFrom(other); + } + return *this; +} + +template +MyQueue::~MyQueue() +{ + free(); +} diff --git a/Sem. 11/Examples/Bar/Source2.cpp b/Sem. 11/Examples/Bar/Source2.cpp new file mode 100644 index 0000000..e27b025 --- /dev/null +++ b/Sem. 11/Examples/Bar/Source2.cpp @@ -0,0 +1,23 @@ +#include +#include "Bar/Bar.h" + +int main() +{ + Bar b; + + b.addDrink(Drink("Coke", 330), 2); //2x Coca cola 330 ml + + b.addDrink(Drink("Fanta", 500)); //1x Fanta 500 ml + + b.addAlcoholDrink(AlcoholDrink("Zagorka", 500, 5)); //1x Zagorka, 5% alc, 500 ml + + std::cout << b.getAlcDrink().getName() << std::endl; // Zagorka + + std::cout << b.getDrink().getName() << std::endl; // Coke + std::cout << b.getDrink().getName() << std::endl; // Coke + std::cout << b.getDrink().getName() << std::endl; // Fanta + + std::cout<< b.getStats().soldAlcoholDrinks << std::endl; // 1 + std::cout << b.getStats().soldDrinks << std::endl; // 3 + +} \ No newline at end of file diff --git a/Sem. 11/Examples/Optional+pair.txt b/Sem. 11/Examples/Optional+pair.txt deleted file mode 100644 index a230c1e..0000000 --- a/Sem. 11/Examples/Optional+pair.txt +++ /dev/null @@ -1 +0,0 @@ -в sem10 са. diff --git a/Sem. 11/Examples/Person, Student, Teacher/Person/Person.cpp b/Sem. 11/Examples/Person, Student, Teacher/Person/Person.cpp new file mode 100644 index 0000000..a0cc714 --- /dev/null +++ b/Sem. 11/Examples/Person, Student, Teacher/Person/Person.cpp @@ -0,0 +1,76 @@ +#pragma once +#include "Person.h" +#include +#pragma warning (disable:4996) + + + +void Person::copyFrom(const Person& other) +{ + name = new char[strlen(other.name) + 1]; + strcpy(name, other.name); + age = other.age; +} + +void Person::free() +{ + delete[] name; +} + +Person::Person(const char* name, int age) +{ + setName(name); + setAge(age); +} + +Person::Person(const Person& other) +{ + copyFrom(other); +} + +Person& Person::operator=(const Person& other) +{ + if (this != &other) + { + free(); + copyFrom(other); + } + return *this; +} +const char* Person::getName() const +{ + return name; +} + +int Person::getAge() const +{ + return age; +} + +void Person::setName(const char* name) +{ + if (name == nullptr || this->name == name) + return; + + delete[] this->name; + size_t nameLen = strlen(name); + this->name = new char[nameLen + 1]; + strcpy(this->name, name); +} + +void Person::setAge(int age) +{ + this->age = age; +} + + +Person::~Person() +{ + free(); +} + +void Person::print() const +{ + std::cout << name << " " << age << std::endl; +} + diff --git a/Sem. 11/Examples/Person, Student, Teacher/Person/Person.h b/Sem. 11/Examples/Person, Student, Teacher/Person/Person.h new file mode 100644 index 0000000..826b671 --- /dev/null +++ b/Sem. 11/Examples/Person, Student, Teacher/Person/Person.h @@ -0,0 +1,27 @@ +#pragma once +#include +class Person +{ + char* name = nullptr; + int age = 0; + + void copyFrom(const Person& other); + void free(); + +public: + Person() = default; + Person(const char* name, int age); + + Person(const Person& other); + Person& operator=(const Person& other); + + const char* getName() const; + int getAge() const; + + ~Person(); + void print() const; + +protected: + void setName(const char* name); + void setAge(int age); +}; diff --git a/Sem. 11/Examples/Person, Student, Teacher/Source.cpp b/Sem. 11/Examples/Person, Student, Teacher/Source.cpp new file mode 100644 index 0000000..c2908c4 --- /dev/null +++ b/Sem. 11/Examples/Person, Student, Teacher/Source.cpp @@ -0,0 +1,21 @@ +#include +#include "Person/Person.h" +#include "Student/Student.h" +#include "Teacher/Teacher.h" + +int main() +{ + char subjects[3][20] = { "English", "Mathematics", "Physics" }; + + char** subjectsPointers = new char* [3]; + subjectsPointers[0] = subjects[0]; + subjectsPointers[1] = subjects[1]; + subjectsPointers[2] = subjects[2]; + + Teacher t("Prof. Ivanov", 33, subjectsPointers, 3); + + std::cout << t.getName() << std::endl; + + delete[] subjectsPointers; //only the pointers - not the data! + +} diff --git a/Sem. 11/Examples/Person, Student, Teacher/Student/Student.cpp b/Sem. 11/Examples/Person, Student, Teacher/Student/Student.cpp new file mode 100644 index 0000000..a79d1fd --- /dev/null +++ b/Sem. 11/Examples/Person, Student, Teacher/Student/Student.cpp @@ -0,0 +1,34 @@ + #include "Student.h" + +Student::Student(const char* name, int age, size_t fn) : Person(name, age) +{ + setFn(fn); +} + +size_t Student::getFn() const +{ + return fn; +} + +void Student::setFn(size_t fn) +{ + + if (fn < 100) + fn = 100; + this->fn = fn; +} + +// The compiler will make: + +//Student(const Student& other) : Person(other), fn(other.fn), grade(other.grade) OK!!! +//{} + +//Student& operator=(const Student& other) OK!!!! +//{ +// if (this != &other) +// { +// Person::operator=(other); //handles the deletion and copying of the base class +// fn = other.fn; +// } +// return *this; +//} \ No newline at end of file diff --git a/Sem. 11/Examples/Person, Student, Teacher/Student/Student.h b/Sem. 11/Examples/Person, Student, Teacher/Student/Student.h new file mode 100644 index 0000000..3e89213 --- /dev/null +++ b/Sem. 11/Examples/Person, Student, Teacher/Student/Student.h @@ -0,0 +1,24 @@ +#include +#include "../Person/Person.h" + +class Student : public Person +{ + size_t fn = 0; +public: + Student() = default; + Student(const char* name, int age, size_t fn); + + size_t getFn() const; + void setFn(size_t fn); + + + // No need for big 4, since there is NO dyn. memory in Student + // The compiler will generate valid: + + //Student(const Student& other); + //Student& operator=(const Student& other); //no need + // ~Student() + + //See on the .cpp file how the compiler creates them + +}; diff --git a/Sem. 11/Examples/Person, Student, Teacher/Teacher/Teacher.cpp b/Sem. 11/Examples/Person, Student, Teacher/Teacher/Teacher.cpp new file mode 100644 index 0000000..d1b6bca --- /dev/null +++ b/Sem. 11/Examples/Person, Student, Teacher/Teacher/Teacher.cpp @@ -0,0 +1,55 @@ +#include "Teacher.h" + +Teacher::Teacher(const char* name, int age, const char* const* subjects, size_t subjectsCount) : Person(name, age) +{ + this->subjects = new char* [subjectsCount]; + + for (size_t i = 0; i < subjectsCount; i++) + { + this->subjects[i] = new char[strlen(subjects[i]) + 1]; + strcpy(this->subjects[i], subjects[i]); + } + this->subjectsCount = subjectsCount; +} + +void Teacher::free() +{ + for (size_t i = 0; i < subjectsCount; i++) + delete[] subjects[i]; + delete[] subjects; +} +void Teacher::copyFrom(const Teacher& other) +{ + subjects = new char* [other.subjectsCount]; + + for (size_t i = 0; i < other.subjectsCount; i++) + { + subjects[i] = new char[strlen(other.subjects[i]) + 1]; + strcpy(subjects[i], other.subjects[i]); + } + + subjectsCount = other.subjectsCount; +} + +Teacher::Teacher(const Teacher& other) : Person(other) +{ + copyFrom(other); //only the teacher stuff is copied +} + +Teacher& Teacher::operator=(const Teacher& other) +{ + if (this != &other) + { + Person::operator=(other); //delete data of person + copy the data from person + free(); //teacher + copyFrom(other); //teacher + } + return *this; +} + + + +Teacher::~Teacher() +{ + free(); +} // destr person diff --git a/Sem. 11/Examples/Person, Student, Teacher/Teacher/Teacher.h b/Sem. 11/Examples/Person, Student, Teacher/Teacher/Teacher.h new file mode 100644 index 0000000..5ef0bd1 --- /dev/null +++ b/Sem. 11/Examples/Person, Student, Teacher/Teacher/Teacher.h @@ -0,0 +1,16 @@ +#include "../Person/Person.h" + +class Teacher : public Person +{ + char** subjects; + size_t subjectsCount; + + void free(); + void copyFrom(const Teacher& other);// само нещата, които са от Teacher +public: + Teacher(const char* name, int age, const char* const * subjects, size_t subjectsCount); + + Teacher(const Teacher& other); + Teacher& operator=(const Teacher& other); + ~Teacher(); +}; \ No newline at end of file diff --git a/Sem. 11/Examples/Person/Person.cpp b/Sem. 11/Examples/Person/Person.cpp deleted file mode 100644 index 439dc2b..0000000 --- a/Sem. 11/Examples/Person/Person.cpp +++ /dev/null @@ -1,78 +0,0 @@ -#pragma once -#include "Person.h" -#include -#pragma warning (disable:4996) - - - -void Person::copyFrom(const Person& other) -{ - name = new char[strlen(other.name) + 1]; - strcpy(name, other.name); - age = other.age; -} - -void Person::free() -{ - delete[] name; -} - -Person::Person(const char* name, int age) -{ - setName(name); - setAge(age); -} - -Person::Person(const Person& other) -{ - copyFrom(other); -} - -Person& Person::operator=(const Person& other) -{ - if (this != &other) - { - free(); - copyFrom(other); - } - return *this; -} -const char* Person::getName() const -{ - return name; -} - -int Person::getAge() const -{ - return age; -} - -void Person::setName(const char* name) -{ - if (name == nullptr || this->name == name) - return; - - delete[] this->name; - size_t nameLen = strlen(name); - this->name = new char[nameLen + 1]; - strcpy(this->name, name); -} - -void Person::setAge(int age) -{ - if(age < 0 || age > 120) - age = 40; - this->age = age; -} - - -Person::~Person() -{ - free(); -} - -void Person::print() const -{ - std::cout << name << " " << age << std::endl; -} - diff --git a/Sem. 11/Examples/Person/Person.h b/Sem. 11/Examples/Person/Person.h deleted file mode 100644 index 826b671..0000000 --- a/Sem. 11/Examples/Person/Person.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once -#include -class Person -{ - char* name = nullptr; - int age = 0; - - void copyFrom(const Person& other); - void free(); - -public: - Person() = default; - Person(const char* name, int age); - - Person(const Person& other); - Person& operator=(const Person& other); - - const char* getName() const; - int getAge() const; - - ~Person(); - void print() const; - -protected: - void setName(const char* name); - void setAge(int age); -}; diff --git a/Sem. 11/Examples/Source2.cpp b/Sem. 11/Examples/Source2.cpp deleted file mode 100644 index b6196dd..0000000 --- a/Sem. 11/Examples/Source2.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include "Student/Student.h" - - - -int main() -{ - Student s2("Fsdf", 34,4); - -} diff --git a/Sem. 11/Examples/Student/Student.cpp b/Sem. 11/Examples/Student/Student.cpp deleted file mode 100644 index 8b5b04c..0000000 --- a/Sem. 11/Examples/Student/Student.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "Student.h" - -Student::Student(const char* name, int age, size_t fn) : Person(name, age) -{ - setFn(fn); -} - -size_t Student::getFn() const -{ - return fn; -} - -void Student::setFn(size_t fn) -{ - - if (fn < 100) - fn = 100; - this->fn = fn; -} - -// The compiler will make: - -//Student(const Student& other) : Person(other), fn(other.fn), grade(other.grade) OK!!! -//{} - -//Student& operator=(const Student& other) OK!!!! -//{ -// if (this != &other) -// { -// Person::operator=(other); //handles the deletion and copying of the base class -// fn = other.fn; -// } -// return *this; -//} \ No newline at end of file diff --git a/Sem. 11/Examples/Student/Student.h b/Sem. 11/Examples/Student/Student.h deleted file mode 100644 index d34763d..0000000 --- a/Sem. 11/Examples/Student/Student.h +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include "../Person/Person.h" - -class Student : public Person -{ - size_t fn = 0; - -public: - Student() = default; - Student(const char* name, int age, size_t fn); - - size_t getFn() const; - void setFn(size_t fn); - - - // No need for big 4, since there is NO dyn. memory in Student - // The compiler will generate valid: - - //Student(const Student& other); - //Student& operator=(const Student& other); //no need - // ~Student() - - //See on the .cpp file how the compiler creates them - -}; -- cgit v1.3.1-23-gd82d