shushus

Спринт 11. Введение в наследование.

Mar 26th, 2023
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. -----------------------------------------------------------------------
  2. people.h
  3. -----------------------------------------------------------------------
  4. #pragma once
  5. #include <string>
  6. #include <set>
  7.  
  8. enum class ProgrammingLanguage { CPP, JAVA, PYTHON, PHP };
  9.  
  10. enum class Gender { MALE, FEMALE };
  11.  
  12. class Person {
  13. public:
  14.     Person(const std::string& name, int age, Gender gender) : name_(name),
  15.                                                               age_(age),
  16.                                                               gender_(gender) {}
  17.     const std::string& GetName() const {
  18.         return name_;
  19.     }
  20.  
  21.     int GetAge() const {
  22.         return age_;
  23.     }
  24.  
  25.     Gender GetGender() const {
  26.         return gender_;
  27.     }
  28.    
  29. private:
  30.     std::string name_;
  31.     int age_;
  32.     Gender gender_;
  33. };
  34.  
  35. class Programmer : public Person {
  36. public:
  37.     Programmer(const std::string& name, int age, Gender gender) : Person(name,
  38.                                                                          age,
  39.                                                                          gender) {}
  40.  
  41.     void AddProgrammingLanguage(ProgrammingLanguage language);
  42.     bool CanProgram(ProgrammingLanguage language) const;
  43.    
  44. private:
  45.     std::set<ProgrammingLanguage> language_;
  46. };
  47.  
  48. enum class WorkerSpeciality { BLACKSMITH, CARPENTER, WOOD_CHOPPER, ENGINEER, PLUMBER };
  49.  
  50. class Worker : public Person {
  51. public:
  52.     Worker(const std::string& name, int age, Gender gender) : Person(name,
  53.                                                                      age,
  54.                                                                      gender) {}
  55.  
  56.     void AddSpeciality(WorkerSpeciality speciality);
  57.     bool HasSpeciality(WorkerSpeciality speciality) const;
  58.    
  59. private:
  60.     std::set<WorkerSpeciality> speciality_;
  61. };
  62. -----------------------------------------------------------------------
  63. people.cpp
  64. -----------------------------------------------------------------------
  65. #include "people.h"
  66.  
  67. #include <stdexcept>
  68.  
  69. using namespace std;
  70.  
  71. void Programmer::AddProgrammingLanguage(ProgrammingLanguage language) {
  72.     language_.insert(language);
  73. }
  74.  
  75. bool Programmer::CanProgram(ProgrammingLanguage language) const {
  76.     return language_.count(language);
  77. }
  78.  
  79. void Worker::AddSpeciality(WorkerSpeciality speciality) {
  80.     speciality_.insert(speciality);
  81. }
  82.  
  83. bool Worker::HasSpeciality(WorkerSpeciality speciality) const {
  84.     return speciality_.count(speciality);
  85. }
  86. -----------------------------------------------------------------------
  87. main.cpp
  88. -----------------------------------------------------------------------
  89. #include "people.h"
  90.  
  91. #include <cassert>
  92. #include <iostream>
  93.  
  94. int main() {
  95.     using namespace std;
  96.  
  97.     try {
  98.         Programmer programmer{"Bjarne Stroustrup"s, 69, Gender::MALE};
  99.         programmer.AddProgrammingLanguage(ProgrammingLanguage::CPP);
  100.         assert(programmer.CanProgram(ProgrammingLanguage::CPP));
  101.  
  102.         Worker worker{"Mario"s, 42, Gender::MALE};
  103.         worker.AddSpeciality(WorkerSpeciality::PLUMBER);
  104.         assert(worker.HasSpeciality(WorkerSpeciality::PLUMBER));
  105.     } catch (...) {
  106.         cout << "Exception was thrown"s << endl;
  107.     }
  108.     cout << "OK"s << endl;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment