From 74c1f5598b4545b029acc45569d29fd0950ee3a8 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Sun, 20 Jun 2021 22:46:32 +0300 Subject: [PATCH 1/7] exercise 1 - creating classes --- lesson_2/exercise_1.hpp | 110 ++++++++++++++++++++++++++++++++++++++++ lesson_2/main.cpp | 15 ++++++ 2 files changed, 125 insertions(+) create mode 100644 lesson_2/exercise_1.hpp create mode 100644 lesson_2/main.cpp diff --git a/lesson_2/exercise_1.hpp b/lesson_2/exercise_1.hpp new file mode 100644 index 0000000..65a5399 --- /dev/null +++ b/lesson_2/exercise_1.hpp @@ -0,0 +1,110 @@ +#ifndef EXERCISE_1_HPP_ +#define EXERCISE_1_HPP_ + +#include + +enum Gender +{ + GENDER_MALE, + GENDER_FEMALE +}; + +class Person +{ +private: + std::string m_name; + int m_age; + Gender m_gender; + float m_weight; +public: + Person(std::string name, int age, Gender gender, float weight) : + m_name(name), m_age(age), m_gender(gender), m_weight(weight) {} + + void setName(std::string name = "") + { + if (name == "") + { + std::cout << "Введите новое имя: "; + std::cin >> m_name; + } + else + m_name = name; + } + + void setAge(int age = 0) + { + if (age == 0) + { + std::cout << "Введите возраст: "; + std::cin >> m_age; + } + else + m_age = age; + } + + void setWeight(float weight = 0.0) + { + if (weight == 0) + { + std::cout << "Введите вес: "; + std::cin >> m_weight; + } + else + m_weight = weight; + } + + void printInfo() const + { + std::cout << "Имя: " << m_name << std::endl + << "Возраст: " << m_age << std::endl + << "Пол: " << (m_gender == GENDER_MALE ? "муж" : "жен") << std::endl + << "Вес: " << m_weight << std::endl; + } +}; + +class Student : public Person +{ +private: + static int count; + int m_yos; // year of study +public: + Student(std::string name, int age, Gender gender, float weight, int yos) : + Person(name, age, gender, weight), m_yos(yos) + { + count++; + } + + static void printCount() + { + std::cout << "Количество студентов: " << count << std::endl; + } + + void setYearStudy(int yos = 0) + { + if (yos == 0) + { + std::cout << "Введите год обучения: "; + std::cin >> m_yos; + } + else + m_yos = yos; + } + + void incYear() + { + m_yos++; + } + + void printInfo() const + { + Person::printInfo(); + std::cout << "Год обучения: " << m_yos << std::endl; + } + + ~Student() + { + count--; + } +}; + +#endif diff --git a/lesson_2/main.cpp b/lesson_2/main.cpp new file mode 100644 index 0000000..522b096 --- /dev/null +++ b/lesson_2/main.cpp @@ -0,0 +1,15 @@ +#include +#include "exercise_1.hpp" + +using namespace std; + +int Student::count = 0; + +int main() +{ + Student Alex("Олег", 20, GENDER_MALE, 75.2, 2020); + Alex.printInfo(); + Student::printCount(); + + return 0; +} -- 2.40.1 From 496554687c3a9387b99646c7dc6fbf7ee495f5b7 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Mon, 21 Jun 2021 10:54:54 +0300 Subject: [PATCH 2/7] adding to vector --- lesson_2/main.cpp | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/lesson_2/main.cpp b/lesson_2/main.cpp index 522b096..ab95dfd 100644 --- a/lesson_2/main.cpp +++ b/lesson_2/main.cpp @@ -1,4 +1,5 @@ #include +#include #include "exercise_1.hpp" using namespace std; @@ -7,9 +8,30 @@ int Student::count = 0; int main() { - Student Alex("Олег", 20, GENDER_MALE, 75.2, 2020); - Alex.printInfo(); - Student::printCount(); +// Student students[] = +// { +// Student("Олег", 20, GENDER_MALE, 75.2, 2020), +// Student("Андрей", 19, GENDER_MALE, 72.8, 2020), +// Student("Анастасия", 20, GENDER_FEMALE, 55.2, 2020), +// Student("Ольга", 21, GENDER_FEMALE, 49.3, 2020), +// Student("Владимир", 19, GENDER_MALE, 69.9, 2020) +// }; +// +// Student::printCount(); +// +// students[0].printInfo(); +// students[2].printInfo(); + + + vector students; + + students.emplace_back("Олег", 20, GENDER_MALE, 75.2, 2020); + students.emplace_back("Андрей", 19, GENDER_MALE, 72.8, 2020); + students.emplace_back("Анастасия", 20, GENDER_FEMALE, 55.2, 2020); + students.emplace_back("Ольга", 21, GENDER_FEMALE, 49.3, 2020); + students.emplace_back("Владимир", 19, GENDER_MALE, 69.9, 2020); + + Student::printCount(); return 0; } -- 2.40.1 From 87404d829e1fe552828bedf931459bf0e60ebbb1 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Mon, 21 Jun 2021 14:22:07 +0300 Subject: [PATCH 3/7] exercise 2 --- lesson_2/exercise_2.hpp | 68 +++++++++++++++++++++++++++++++++++++++++ lesson_2/main.cpp | 25 ++++++++++----- 2 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 lesson_2/exercise_2.hpp diff --git a/lesson_2/exercise_2.hpp b/lesson_2/exercise_2.hpp new file mode 100644 index 0000000..ad31e0e --- /dev/null +++ b/lesson_2/exercise_2.hpp @@ -0,0 +1,68 @@ +#ifndef EXERCISE_2_HPP_ +#define EXERCISE_2_HPP_ + +#include + +class Fruit +{ +private: + std::string m_name; + std::string m_color; +public: + void setName(std::string name) + { + m_name = name; + } + + void setColor(std::string color) + { + m_color = color; + } + + const std::string& getName() const + { + return m_name; + } + + const std::string& getColor() const + { + return m_color; + } +}; + +class Apple : public Fruit +{ +public: + Apple(std::string color = "") + { + if (color == "") + color = "green"; + + setName("apple"); + setColor(color); + } +}; + +class Banana : public Fruit +{ +public: + Banana(std::string color = "") + { + if (color == "") + color = "yellow"; + + setName("banana"); + setColor(color); + } +}; + +class GrannySmith : public Apple +{ +public: + GrannySmith() + { + setName("Granny Smith " + Apple::getName()); + } +}; + +#endif diff --git a/lesson_2/main.cpp b/lesson_2/main.cpp index ab95dfd..ac46e74 100644 --- a/lesson_2/main.cpp +++ b/lesson_2/main.cpp @@ -1,6 +1,7 @@ #include #include #include "exercise_1.hpp" +#include "exercise_2.hpp" using namespace std; @@ -23,15 +24,25 @@ int main() // students[2].printInfo(); - vector students; +// vector students; +// +// students.emplace_back("Олег", 20, GENDER_MALE, 75.2, 2020); +// students.emplace_back("Андрей", 19, GENDER_MALE, 72.8, 2020); +// students.emplace_back("Анастасия", 20, GENDER_FEMALE, 55.2, 2020); +// students.emplace_back("Ольга", 21, GENDER_FEMALE, 49.3, 2020); +// students.emplace_back("Владимир", 19, GENDER_MALE, 69.9, 2020); +// +// Student::printCount(); - students.emplace_back("Олег", 20, GENDER_MALE, 75.2, 2020); - students.emplace_back("Андрей", 19, GENDER_MALE, 72.8, 2020); - students.emplace_back("Анастасия", 20, GENDER_FEMALE, 55.2, 2020); - students.emplace_back("Ольга", 21, GENDER_FEMALE, 49.3, 2020); - students.emplace_back("Владимир", 19, GENDER_MALE, 69.9, 2020); + // Exercise 2 - Student::printCount(); + Apple a("red"); + Banana b; + GrannySmith c; + + std::cout << "My " << a.getName() << " is " << a.getColor() << ".\n"; + std::cout << "My " << b.getName() << " is " << b.getColor() << ".\n"; + std::cout << "My " << c.getName() << " is " << c.getColor() << ".\n"; return 0; } -- 2.40.1 From c3e27642b2d184c2ae46393a67604de14e73c9b4 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Tue, 22 Jun 2021 14:38:06 +0300 Subject: [PATCH 4/7] lesson 2 --- lesson_2/main.cpp | 81 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 59 insertions(+), 22 deletions(-) diff --git a/lesson_2/main.cpp b/lesson_2/main.cpp index ac46e74..fb554e1 100644 --- a/lesson_2/main.cpp +++ b/lesson_2/main.cpp @@ -9,30 +9,19 @@ int Student::count = 0; int main() { -// Student students[] = -// { -// Student("Олег", 20, GENDER_MALE, 75.2, 2020), -// Student("Андрей", 19, GENDER_MALE, 72.8, 2020), -// Student("Анастасия", 20, GENDER_FEMALE, 55.2, 2020), -// Student("Ольга", 21, GENDER_FEMALE, 49.3, 2020), -// Student("Владимир", 19, GENDER_MALE, 69.9, 2020) -// }; -// -// Student::printCount(); -// -// students[0].printInfo(); -// students[2].printInfo(); + Student students[] = + { + Student("Олег", 20, GENDER_MALE, 75.2, 2020), + Student("Андрей", 19, GENDER_MALE, 72.8, 2020), + Student("Анастасия", 20, GENDER_FEMALE, 55.2, 2020), + Student("Ольга", 21, GENDER_FEMALE, 49.3, 2020), + Student("Владимир", 19, GENDER_MALE, 69.9, 2020) + }; + students[0].printInfo(); + students[2].printInfo(); -// vector students; -// -// students.emplace_back("Олег", 20, GENDER_MALE, 75.2, 2020); -// students.emplace_back("Андрей", 19, GENDER_MALE, 72.8, 2020); -// students.emplace_back("Анастасия", 20, GENDER_FEMALE, 55.2, 2020); -// students.emplace_back("Ольга", 21, GENDER_FEMALE, 49.3, 2020); -// students.emplace_back("Владимир", 19, GENDER_MALE, 69.9, 2020); -// -// Student::printCount(); + Student::printCount(); // Exercise 2 @@ -44,5 +33,53 @@ int main() std::cout << "My " << b.getName() << " is " << b.getColor() << ".\n"; std::cout << "My " << c.getName() << " is " << c.getColor() << ".\n"; + // Exercise 3 + /* + Для реализации игры в Блэкджек необходимы данные объекты: + + 1) Колода карт + 2) Игрок + 3) Дилер + + Исходя из указанных объектов можно определить несколько классов: + + 1) Карта + - номинал + - масть + 2) Колода + - массив ссылок на объекты класса Карта + 3) Игрок (абстрактный класс) + - Имя игрока + 4) Игрок (человек, потомок от абстрактного класса Игрок) + 5) Дилер (человек, потомок от абстрактного класса Игрок) + 6) Рука (набор карт) + - массив ссылок на карты каждого игрока + 7) Игра + - текущая Колода + - текущий Дилер + - текущий Игрок + */ + return 0; } + + + + + + + + + + + + + + + + + + + + + -- 2.40.1 From e9026cb63dde16cf8340ce3ff1a35ea7d0cc0045 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Tue, 22 Jun 2021 19:28:40 +0300 Subject: [PATCH 5/7] added descriptions --- lesson_2/main.cpp | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/lesson_2/main.cpp b/lesson_2/main.cpp index fb554e1..8cbbf53 100644 --- a/lesson_2/main.cpp +++ b/lesson_2/main.cpp @@ -9,6 +9,8 @@ int Student::count = 0; int main() { + // Exercise 1 + Student students[] = { Student("Олег", 20, GENDER_MALE, 75.2, 2020), @@ -62,24 +64,3 @@ int main() return 0; } - - - - - - - - - - - - - - - - - - - - - -- 2.40.1 From 6c7f00e7f7dc63d7ddfb40c8a109e5cca80d6971 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Wed, 23 Jun 2021 14:27:19 +0300 Subject: [PATCH 6/7] edit exercise 1 --- lesson_2/exercise_1.hpp | 7 +++++++ lesson_2/main.cpp | 19 +++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lesson_2/exercise_1.hpp b/lesson_2/exercise_1.hpp index 65a5399..29fd2bf 100644 --- a/lesson_2/exercise_1.hpp +++ b/lesson_2/exercise_1.hpp @@ -74,6 +74,13 @@ public: count++; } + Student(const Student &s) : Person(s) + { + count++; + + m_yos = s.m_yos; + } + static void printCount() { std::cout << "Количество студентов: " << count << std::endl; diff --git a/lesson_2/main.cpp b/lesson_2/main.cpp index fb554e1..e9935a3 100644 --- a/lesson_2/main.cpp +++ b/lesson_2/main.cpp @@ -9,17 +9,16 @@ int Student::count = 0; int main() { - Student students[] = - { - Student("Олег", 20, GENDER_MALE, 75.2, 2020), - Student("Андрей", 19, GENDER_MALE, 72.8, 2020), - Student("Анастасия", 20, GENDER_FEMALE, 55.2, 2020), - Student("Ольга", 21, GENDER_FEMALE, 49.3, 2020), - Student("Владимир", 19, GENDER_MALE, 69.9, 2020) - }; + vector students; - students[0].printInfo(); - students[2].printInfo(); + students.emplace_back("Олег", 20, GENDER_MALE, 75.2, 2020); + students.emplace_back("Андрей", 19, GENDER_MALE, 72.8, 2020); + + Student::printCount(); + + students.emplace_back("Анастасия", 20, GENDER_FEMALE, 55.2, 2020); + students.emplace_back("Ольга", 21, GENDER_FEMALE, 49.3, 2020); + students.emplace_back("Владимир", 19, GENDER_MALE, 69.9, 2020); Student::printCount(); -- 2.40.1 From 246e6541c8c539e458755d459a7978a131f6e11c Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Wed, 23 Jun 2021 14:32:29 +0300 Subject: [PATCH 7/7] changed the first task --- lesson_2/main.cpp | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/lesson_2/main.cpp b/lesson_2/main.cpp index 4ace2e3..2a8215a 100644 --- a/lesson_2/main.cpp +++ b/lesson_2/main.cpp @@ -9,7 +9,6 @@ int Student::count = 0; int main() { -<<<<<<< HEAD vector students; students.emplace_back("Олег", 20, GENDER_MALE, 75.2, 2020); @@ -20,21 +19,6 @@ int main() students.emplace_back("Анастасия", 20, GENDER_FEMALE, 55.2, 2020); students.emplace_back("Ольга", 21, GENDER_FEMALE, 49.3, 2020); students.emplace_back("Владимир", 19, GENDER_MALE, 69.9, 2020); -======= - // Exercise 1 - - Student students[] = - { - Student("Олег", 20, GENDER_MALE, 75.2, 2020), - Student("Андрей", 19, GENDER_MALE, 72.8, 2020), - Student("Анастасия", 20, GENDER_FEMALE, 55.2, 2020), - Student("Ольга", 21, GENDER_FEMALE, 49.3, 2020), - Student("Владимир", 19, GENDER_MALE, 69.9, 2020) - }; - - students[0].printInfo(); - students[2].printInfo(); ->>>>>>> e9026cb63dde16cf8340ce3ff1a35ea7d0cc0045 Student::printCount(); -- 2.40.1