diff options
11 files changed, 330 insertions, 0 deletions
| -rw-r--r-- | Big4_and_inheritance/Drinks/AlcoholDrink.cpp | 18 | ||||
| -rw-r--r-- | Big4_and_inheritance/Drinks/AlcoholDrink.h | 14 | ||||
| -rw-r--r-- | Big4_and_inheritance/Drinks/Drink.cpp | 63 | ||||
| -rw-r--r-- | Big4_and_inheritance/Drinks/Drink.h | 21 | ||||
| -rw-r--r-- | Big4_and_inheritance/Drinks/main.cpp | 10 | ||||
| -rw-r--r-- | Big4_and_inheritance/Students/GradesBook.cpp | 91 | ||||
| -rw-r--r-- | Big4_and_inheritance/Students/GradesBook.h | 27 | ||||
| -rw-r--r-- | Big4_and_inheritance/Students/Student.cpp | 10 | ||||
| -rw-r--r-- | Big4_and_inheritance/Students/Student.h | 15 | ||||
| -rw-r--r-- | Big4_and_inheritance/Students/User.cpp | 43 | ||||
| -rw-r--r-- | Big4_and_inheritance/Students/User.h | 18 |
diff --git a/Big4_and_inheritance/Drinks/AlcoholDrink.cpp b/Big4_and_inheritance/Drinks/AlcoholDrink.cpp new file mode 100644 index 0000000..0e20087 --- /dev/null +++ b/Big4_and_inheritance/Drinks/AlcoholDrink.cpp @@ -0,0 +1,18 @@ +#include "AlcoholDrink.h" + + + +AlcoholDrink::AlcoholDrink(const char* mark, int ml, int percentAlcohol) : Drink(mark,ml) +{ + setAlcohol(percentAlcohol); +} +void AlcoholDrink::setAlcohol(int alcohol) +{ + if (alcohol < 4 || alcohol>98) + alcohol = 20; + percentAlcohol = alcohol; +} +int AlcoholDrink::getAlcohol() const +{ + return percentAlcohol; +} diff --git a/Big4_and_inheritance/Drinks/AlcoholDrink.h b/Big4_and_inheritance/Drinks/AlcoholDrink.h new file mode 100644 index 0000000..0bd1d10 --- /dev/null +++ b/Big4_and_inheritance/Drinks/AlcoholDrink.h @@ -0,0 +1,14 @@ +#pragma once + +#include "Drink.h" + +class AlcoholDrink : public Drink +{ +private: + int percentAlcohol; + +public: + AlcoholDrink(const char* mark, int ml, int percentAlcohol); + void setAlcohol(int alcohol); + int AlcoholDrink::getAlcohol() const; +}; diff --git a/Big4_and_inheritance/Drinks/Drink.cpp b/Big4_and_inheritance/Drinks/Drink.cpp new file mode 100644 index 0000000..709446b --- /dev/null +++ b/Big4_and_inheritance/Drinks/Drink.cpp @@ -0,0 +1,63 @@ +#include "Drink.h" + + +void Drink::CopyFrom(const Drink& other) +{ + int len = strlen(other.mark); + mark = new char[len + 1]; + strcpy(mark, other.mark); + + ml = other.ml; +} +void Drink::Free() +{ + delete[] mark; + ml = 0; +} + +Drink::Drink() +{ + mark = new char[6]; + strcpy(mark, "Water"); + ml = 500; +} +Drink::Drink(const char* mark, int ml) +{ + int len = strlen(mark); + this->mark = new char[len + 1]; + strcpy(this->mark, mark); + + 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::setML(int ml) +{ + if (ml < 100 || ml> 1000) + ml = 500; + this->ml = ml; +} +int Drink::getML() const +{ + return ml; +} +const char* Drink::getMark() const +{ + return mark; +} diff --git a/Big4_and_inheritance/Drinks/Drink.h b/Big4_and_inheritance/Drinks/Drink.h new file mode 100644 index 0000000..db93c97 --- /dev/null +++ b/Big4_and_inheritance/Drinks/Drink.h @@ -0,0 +1,21 @@ +#pragma once +#include <iostream> + +class Drink +{ +private: + char* mark; + int ml; + void CopyFrom(const Drink& other); + void Free(); +public: + Drink(); + Drink(const char* mark, int ml); + Drink(const Drink& other); + Drink& operator=(const Drink& other); + ~Drink(); + + void setML(int ml); + int getML() const; + const char* getMark() const; +}; diff --git a/Big4_and_inheritance/Drinks/main.cpp b/Big4_and_inheritance/Drinks/main.cpp new file mode 100644 index 0000000..c809123 --- /dev/null +++ b/Big4_and_inheritance/Drinks/main.cpp @@ -0,0 +1,10 @@ +#include "AlcoholDrink.h" +int main() +{ + AlcoholDrink DR1("Spirt", 100, 98); + + AlcoholDrink dr2 (DR1); + dr2.getMark(); + + +} diff --git a/Big4_and_inheritance/Students/GradesBook.cpp b/Big4_and_inheritance/Students/GradesBook.cpp new file mode 100644 index 0000000..6d3de27 --- /dev/null +++ b/Big4_and_inheritance/Students/GradesBook.cpp @@ -0,0 +1,91 @@ +#include "GradesBook.h" + + +GradesBook::GradesBook() +{ + capacity = 4; + grades = new int[capacity]; + size = 0; +} +GradesBook::GradesBook(const GradesBook& other) +{ + std::cout << "Copy contr of GradesBook" << std::endl; + CopyFrom(other); +} +GradesBook& GradesBook::operator=(const GradesBook& other) +{ + if (this != &other) + { + Free(); + CopyFrom(other); + } + return *this; +} +GradesBook::~GradesBook() +{ + Free(); +} + + +void GradesBook::CopyFrom(const GradesBook& other) +{ + grades = new int[other.capacity]; + for (int i = 0; i < other.size; i++) + grades[i] = other.grades[i]; + capacity = other.capacity; + size = other.size; +} +void GradesBook::Free() +{ + delete[] grades; +} + + +bool GradesBook::isFull() +{ + return size == capacity; +} +void GradesBook::AddGrade(int grade) +{ + if (!isValidGrade(grade)) + return; + if (isFull()) + Resize(); + grades[size++] = grade; + +} +void GradesBook::Resize() +{ + int* temp = new int[capacity * 2]; + for (int i = 0; i < size; i++) + temp[i] = grades[i]; + capacity *= 2; + delete[] grades; + grades = temp; +} +void GradesBook::RemoveLastGrade() +{ + size--; +} +bool GradesBook::isValidGrade(int grade) +{ + return (grade >= 2 && grade <= 6); +} +int GradesBook::operator[](int index) const +{ + if (index < 0 || index >= size) + return -1; + return grades[index]; +} + +std::ostream& operator<<(std::ostream& stream, GradesBook& obj) +{ + for (int i = 0; i < obj.capacity; i++) + { + if (i < obj.size) + stream << obj.grades[i] << ' '; + else + stream << '_' << ' '; + } + return stream; +}
\ No newline at end of file diff --git a/Big4_and_inheritance/Students/GradesBook.h b/Big4_and_inheritance/Students/GradesBook.h new file mode 100644 index 0000000..e63e92f --- /dev/null +++ b/Big4_and_inheritance/Students/GradesBook.h @@ -0,0 +1,27 @@ +#pragma once +#include <iostream> + +class GradesBook +{ + int* grades; + int size; + int capacity; + + bool isValidGrade(int grade); + void Resize(); + void CopyFrom(const GradesBook& other); + void Free(); +public: + GradesBook(); + GradesBook(const GradesBook& other); + GradesBook& operator=(const GradesBook& other); + ~GradesBook(); + + bool isFull(); + void AddGrade(int grade); + void RemoveLastGrade(); + + int operator[](int index) const; + friend std::ostream& operator<<(std::ostream& stream, GradesBook& obj); + +}; diff --git a/Big4_and_inheritance/Students/Student.cpp b/Big4_and_inheritance/Students/Student.cpp new file mode 100644 index 0000000..4734529 --- /dev/null +++ b/Big4_and_inheritance/Students/Student.cpp @@ -0,0 +1,10 @@ +#include "Student.h" +Student::Student(const char* name, int age, int fn): User(name,age) +{ + this->fn = fn; +} + +void Student::ReceiveGrade(int grade) +{ + b.AddGrade(grade); +}
\ No newline at end of file diff --git a/Big4_and_inheritance/Students/Student.h b/Big4_and_inheritance/Students/Student.h new file mode 100644 index 0000000..aafaf58 --- /dev/null +++ b/Big4_and_inheritance/Students/Student.h @@ -0,0 +1,15 @@ +#pragma once +#include "User.h" +#include "GradesBook.h" + +class Student : public User +{ +public: + int fn; + GradesBook b; + + Student(const char* name, int age, int fn); + void ReceiveGrade(int grade); + + +}; diff --git a/Big4_and_inheritance/Students/User.cpp b/Big4_and_inheritance/Students/User.cpp new file mode 100644 index 0000000..93d34b6 --- /dev/null +++ b/Big4_and_inheritance/Students/User.cpp @@ -0,0 +1,43 @@ +#include "User.h" + +/* +User::User(): name(nullptr),age(10) +{ + +} +*/ + +User::User(const char* name, int age) +{ + this->name = new char[strlen(name) + 1]; + strcpy(this->name, name); + this->age = age; +} +User::User(const User& other) +{ + cout << "Copy contr of user" << endl; + CopyFrom(other); +} +User& User::operator=(const User& other) +{ + if (this != &other) + { + Free(); + CopyFrom(other); + } + return *this; +} +User::~User() +{ + Free(); +} +void User::CopyFrom(const User& other) +{ + name = new char[strlen(other.name) + 1]; + strcpy(name, other.name); + age = other.age; +} +void User::Free() +{ + delete[] name; +}
\ No newline at end of file diff --git a/Big4_and_inheritance/Students/User.h b/Big4_and_inheritance/Students/User.h new file mode 100644 index 0000000..400057b --- /dev/null +++ b/Big4_and_inheritance/Students/User.h @@ -0,0 +1,18 @@ +#pragma once + +#include <iostream> +using namespace std; +class User +{ +private: + char* name; + int age; + void CopyFrom(const User& other); + void Free(); +public: + //User(); + User(const char*name, int age); + User(const User& other); + User& operator=(const User& other); + ~User(); +}; |
