Advertisement
35657

Untitled

May 14th, 2024
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 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() {
  12.         cout << "Конструткор класса Person" << endl;
  13.     };
  14.  
  15.     string GetName() const {
  16.         return name_;
  17.     }
  18.     int GetAge() const {
  19.         return age_;
  20.     }
  21.     string GetGender() const {
  22.         return gender_;
  23.     }
  24.  
  25.     virtual ~Person() = 0; // сделали деструктор чисто виртуальным (единственное отличие от обычной функции - необходимость выноса за класс объявления деструктора (в строке 33))
  26.  
  27. private:
  28.     string name_;
  29.     string gender_;
  30.     int age_;
  31. };
  32.  
  33. Person::~Person(){}
  34.  
  35.  
  36. // Рабочий
  37. class Worker : public Person {
  38. public:
  39.     Worker() {
  40.         cout << "Конструткор класса Worker" << endl;
  41.     };
  42.  
  43.     void AddSpeciality(string speciality) {
  44.         specialties_.insert(speciality);
  45.     }
  46.     bool HasSpeciality(string speciality) const {
  47.         return specialties_.count(speciality);
  48.     }
  49.  
  50.     ~Worker() override {
  51.         cout << "Деструктор класса Worker (в нем удаляются объекты в динамической памяти)" << endl;
  52.     };
  53.  
  54. private:
  55.     set<string> specialties_;
  56. };
  57.  
  58.  
  59.  
  60. int main() {
  61.     setlocale(LC_ALL, "ru");
  62.  
  63.     //Person pers; // класс Person стал абстрактным из-за чисто виртуального деструктора, создавать его экземпляры нельзя
  64.  
  65.     Person* pr = new Worker;
  66.  
  67.     delete pr;
  68.  
  69.     cout << endl << endl;
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement