(I am a newbie, new C ++) In general, the base class and the derived class (that is, the parent class and subclass) both contain Constructors (called when the class is created) and destructor (called when a class is deleted ).
What is the sequence of the constructor and destructor that a subclass calls when defining an object?
This is the code in Case 1:
# Include <iostream> using namespace STD; Class A {public: A () {cout <This <"New Super \ n ";}~ A () {cout <This <"delete super \ n" ;}}; Class B: Public A {public: B () {cout <This <"new sub \ n ";}~ B () {cout <This <"delete sub \ n" ;}}; int main (INT ARGs, char * argv []) {B; cout <& B <Endl;/** or: * B = new B (); cout <B <Endl; Delete B ;**/}
The result after running is:
It can be seen that when the initialization subclass is the first initialization parent class, and when the release is the first subclass, the parent class.