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; }