Advertisement
35657

Untitled

Aug 30th, 2024
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 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.     // добавили методов программисту для обращения полям родительского класса
  48.     void PrintName() const {
  49.         cout << name_ << endl;
  50.     }
  51.     void PrintAge() const {
  52.         cout << age_ << endl;
  53.     }
  54.     /*void PrintGender() const {
  55.         cout << gender_ << endl;
  56.     }*/
  57.  
  58. private:
  59.     set<string> programming_languages_;
  60. };
  61.  
  62.  
  63. // Рабочий. Владеет несколькими специальностями
  64. class Worker : public Person {
  65. public:
  66.     Worker(string name, int age, string gender) : Person(name, age, gender) {};
  67.  
  68.     void AddSpeciality(string speciality) {
  69.         specialties_.insert(speciality);
  70.     }
  71.     bool HasSpeciality(string speciality) const {
  72.         return specialties_.count(speciality);
  73.     }
  74.  
  75. private:
  76.     set<string> specialties_;
  77. };
  78.  
  79. int main() {
  80.     setlocale(LC_ALL, "ru");
  81.     Programmer pr("Иван", 22, "man");
  82.     Worker wk("Гена", 23, "man");
  83.     pr.PrintName();
  84.     pr.PrintAge();
  85.  
  86.     // поле public
  87.     // cout << pr.name_ << endl; // доступно из внешнего кода (через точку)
  88.     // cout << pr.GetName() << endl;// можем обращаться внутри родительского класса (GetName())
  89.     // pr.PrintName();// можем обращаться внутри дочернего класса (PrintName())
  90.  
  91.     // поле private
  92.     //cout << pr.gender_ << endl; // здесь ошибка - не доступно из внешнего кода (через точку)
  93.     //cout << pr.GetGender() << endl; //можем обращаться внутри родительского класса (GetGender())
  94.     //pr.PrintGender(); // не можем обращаться внутри дочернего класса(PrintGender() не заработает)
  95.  
  96.     // поле protected
  97.     //cout << pr.age_ << endl; // здесь ошибка - не доступно из внешнего кода (через точку)
  98.     //cout << pr.GetAge() << endl;// можем обращаться внутри родительского класса (GetAge())
  99.     //pr.PrintAge();// можем обращаться внутри дочернего класса(PrintAge())
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement