Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <set>
- #include <string>
- #include <iostream>
- using namespace std;
- // Человек
- class Person {
- public:
- Person() {
- cout << "Конструткор класса Person" << endl;
- };
- string GetName() const {
- return name_;
- }
- int GetAge() const {
- return age_;
- }
- string GetGender() const {
- return gender_;
- }
- virtual ~Person() = 0; // сделали деструктор чисто виртуальным (единственное отличие от обычной функции - необходимость выноса за класс объявления деструктора (в строке 33))
- private:
- string name_;
- string gender_;
- int age_;
- };
- Person::~Person(){}
- // Рабочий
- class Worker : public Person {
- public:
- Worker() {
- cout << "Конструткор класса Worker" << endl;
- };
- void AddSpeciality(string speciality) {
- specialties_.insert(speciality);
- }
- bool HasSpeciality(string speciality) const {
- return specialties_.count(speciality);
- }
- ~Worker() override {
- cout << "Деструктор класса Worker (в нем удаляются объекты в динамической памяти)" << endl;
- };
- private:
- set<string> specialties_;
- };
- int main() {
- setlocale(LC_ALL, "ru");
- //Person pers; // класс Person стал абстрактным из-за чисто виртуального деструктора, создавать его экземпляры нельзя
- Person* pr = new Worker;
- delete pr;
- cout << endl << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement