# Include <iostream>
# Include <string>
# Include <cstdlib>
/*
Construction and initialization sequence:
1. initialize the class members of the base class
2. base class Constructor
3. class member initialization of a derived class
4. derived class Constructor
Destruction order:
1. Subclass destructor
2. Subclass class member analysis and destruction
3. destructor of the derived class
4. destructor and destruction of class members of a derived class
These two sequences can be understood through the inbound and outbound operations ~ The order is the opposite.
*/
Using namespace STD;
Class C
{
Public:
C () {cout <"Class C construct" <Endl ;}
~ C () {cout <"Class C destruct" <Endl ;}
};
Class base
{
Public:
Base () {cout <"base construct" <Endl ;}
~ Base () {cout <"Base destruct" <Endl ;}
C OBJ;
};
Class D
{
Public:
D () {cout <"Class D construct" <Endl ;}
~ D () {cout <"Class D destruct" <Endl ;}
};
Class child: public Base
{
Public:
Child () {cout <"Child construct" <Endl ;}
~ Child () {cout <"Child destruct" <Endl ;}
D obj1;
};
Int main ()
{
Child OBJ;
}
Execution result:
Class C construct
Base construct
Class D construct
Child construct
Child destruct
Class D destruct
Base destruct
Class C destruct