Advertisement
35657

Untitled

Apr 30th, 2024
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.43 KB | None | 0 0
  1. #include <set>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. // Человек
  9. class Person {
  10. public:
  11.     Person(string name, int age, string gender) : name_(name), age_(age), gender_(gender) {};
  12.  
  13.     string GetName() const {
  14.         return name_;
  15.     }
  16.     int GetAge() const {
  17.         return age_;
  18.     }
  19.     string GetGender() const {
  20.         return gender_;
  21.     }
  22.     // изменили расположение полей в родительском классе
  23.     string name_;
  24.  
  25. protected:
  26.     int age_;
  27.  
  28. private:
  29.     string gender_;
  30. };
  31.  
  32.  
  33.  
  34. // Программист. Знает несколько языков программирования
  35. class Programmer : public Person {
  36. public:
  37.     Programmer(string name, int age, string gender) : Person(name, age, gender) {}; // если нет конструктора по умолчанию, а есть параметризованный конструктор, то обязательно вызываем его
  38.  
  39.     void AddProgrammingLanguage(string language) {
  40.         programming_languages_.insert(language);
  41.     }
  42.     bool CanProgram(string language) const {
  43.         return programming_languages_.count(language);
  44.     }
  45.  
  46.     // добавили методов программисту для обращения полям родительского класса
  47.     void PrintName() const {
  48.         cout << name_ << endl;
  49.     }
  50.     void PrintAge() const {
  51.         cout << age_ << endl;
  52.     }
  53.     /*void PrintGender() const {
  54.         cout << gender_ << endl;
  55.     }*/
  56.  
  57. private:
  58.     set<string> programming_languages_;
  59. };
  60.  
  61.  
  62. // Рабочий. Владеет несколькими специальностями
  63. class Worker : public Person {
  64. public:
  65.     Worker(string name, int age, string gender) : Person(name, age, gender) {};
  66.  
  67.     void AddSpeciality(string speciality) {
  68.         specialties_.insert(speciality);
  69.     }
  70.     bool HasSpeciality(string speciality) const {
  71.         return specialties_.count(speciality);
  72.     }
  73.  
  74. private:
  75.     set<string> specialties_;
  76. };
  77.  
  78. int main() {
  79.     setlocale(LC_ALL, "ru");
  80.     Programmer pr("Иван", 22, "man");
  81.     Worker wk("Гена", 23, "man");
  82.  
  83.     // поле public
  84.     //cout << pr.name_ << endl; // доступно из внешнего кода (через точку)
  85.     //cout << pr.GetName() << endl;// можем обращаться внутри родительского класса (GetName())
  86.     //pr.PrintName();// можем обращаться внутри дочернего класса (PrintName())
  87.  
  88.     // поле private
  89.     //cout << pr.gender_ << endl; // здесь ошибка - не доступно из внешнего кода (через точку)
  90.     //cout << pr.GetGender() << endl; //можем обращаться внутри родительского класса (GetGender())
  91.     //pr.PrintGender(); // не можем обращаться внутри дочернего класса(PrintGender() не заработает)
  92.  
  93.     // поле protected
  94.     //cout << pr.age_ << endl; // здесь ошибка - не доступно из внешнего кода (через точку)
  95.     //cout << pr.GetAge() << endl;// можем обращаться внутри родительского класса (GetAge())
  96.     //pr.PrintAge();// можем обращаться внутри дочернего класса(PrintAge())
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement