Good evening, there are a couple of questions on polymorphism.
1 question:
#include <iostream> #include <string> using namespace std; class Human{ protected: string name; int age; char sex; public: /* v Ваш код v */ Human(string name, int age, char sex) { this-> name = name; this-> age = age; this-> sex = sex; cout << "контструктор Human" << endl; } virtual void Show() { cout << name << endl << age << endl << sex << endl; } virtual ~Human() { cout << "виртуальный деструктор Human" << endl; } }; class Coder: public Human{ protected: float IQ; public: /* v Ваш код v */ Coder(string name, int age, char sex, float IQ):Human(name, age, sex) { this-> IQ = IQ; cout << "конструктор Coder" << endl; } void Show() { cout << IQ << endl; } ~Coder() { cout << "виртуальный деструктор Coder" << endl; } }; int main(){ setlocale(LC_ALL, "RUS"); Human *human = new Human("Tom", 21, 'M'); Human *coder = new Coder("Alice", 19, 'F', 150.6f); human->Show(); coder->Show(); delete human; delete coder; return 0; } /* Консоль: конструктор Human конструктор Human конструктор Coder Tom 21 M 150.6 виртуальный деструктор Human виртуальный деструктор Coder виртуальный деструктор Human */ As is known, the destruction of an object of the base class entails the destruction of the object of the derived class, so the virtual destructor of the derived object must be performed before the destructor of the base object.
Therefore, in this case, the objects should be deleted in this order:
delete coder; delete human; Or does the removal order not matter, since these are 2 different objects?
those. when creating an object of a successor — the base constructor is first called, then the successor, and when creating a base object, the base constructor is called and the order of deleting objects does not matter as the order of their creation.
And the phrase “the destruction of a base class object entails the destruction of a derived class object” only applies when it comes to polymorphism?
those. in the case of delete coder this phrase makes sense, since the object of the heir is removed through a pointer to the base class, so the virtual destructor is used
and in the case of delete human , the base class object is deleted from the dynamic memory; therefore, it uses not a virtual destructor, but a normal destructor, which in this case is defined in the class body as a virtual
Confusion arose due to the fact that if you create these objects in automatic memory, then the destructors are called in a logical order (according to the principle, the last one came - the first left):
Human h("Tom", 21, 'M'); Human *human = &h; Coder c("Alice", 19, 'F', 150.6f); Human *coder = &c; human->Show(); coder->Show(); /* Консоль: конструктор Human конструктор Human конструктор Coder Tom 21 M 150.6 виртуальный деструктор Coder виртуальный деструктор Human виртуальный деструктор Human */ 2 question:
To implement polymorphism, you can:
create a heir object in dynamic memory:
Human *p = new Coder; create a successor object in automatic memory:
Coder obj; Human *p = &obj; Here I have a contradiction, polymorphism is achieved only with the help of late binding, which means that the object must be created in dynamic memory using the new operator, because it is during the execution of the program that the compiler analyzes the type of object pointed to by the pointer in order to determine which implementation of the virtual method should be called, during the launch of the program this is still unknown.
In this connection, the question is why the creation of an object in automatic memory is considered correct for the implementation of polymorphism?