When I was asked about this during the interview yesterday, I was so ashamed that I had mixed up and regretted it. The decision should be thoroughly clarified. This is also a benefit.
First, let's talk about the constructor. We all know that the constructor can call member variables, and the Child class in inheritance is to turn the members of the base class into their own members, that is to say, the subclass can call the members of the base class in the constructor. This means that the base class constructor must be called before creating the subclass, only in this way can the subclass use the base class members in the constructor. Therefore, when creating a subclass, the base class constructor is called before calling its own constructor. In layman's terms, you must use certain items, but you cannot produce these items by yourself. Naturally, you must wait for others to produce them before using them.
The next step is the destructor. As mentioned above, the subclass turns the base class members into its own members, then the base class will only exist in the subclass until the subclass calls the destructor. Make a hypothesis: If the destructor of the base class is called before the subclass, what will happen? The class member is terminated, but the class itself is still there. But if the class exists, the class member should still exist. Isn't that a conflict? Therefore, the subclass calls its own destructor and then calls the destructor of the base class.
Now the virtual function is used, and the main role of virtual is in polymorphism, and the major cause of C ++ polymorphism is the dynamic binding of classes, dynamic binding refers to converting the pointer or reference of a subclass into a base class object, so that a base class object can dynamically determine which subclass member function to call. This indicates that virtual has no meaning (except pure virtual functions) if there is no subclass pointer or reference conversion for the base class object, that is, whether or not virtual is to call its own member functions. Through these analyses, you will have an eye on virtual. When a subclass pointer or reference is converted to a base class, if a virtual defined function is used in the base class, after the quilt class is rewritten, the base class object will call the rewritten function in the subclass according to the subclass, instead of functions in the base class; otherwise, if the base class is not defined by virtual, no matter which subclass value the base class is assigned, all base-class member functions are called (of course, the base-class function that is overloaded by the value subclass, otherwise the member functions that are unique to the subclass will not be compiled ).
This interview really gave me the experience of understanding the meaning of the language, rather than simply remembering it. Otherwise, we will see the situation of mixing up yesterday. Infinite emotion! If it is not good or wrong, you are welcome to make a brick.