1.

If class D is derived from a base class B. When creating an object of type D in what order would the constructors of these classes get called?

Answer»

The derived CLASS has two parts, a base part, and a derived part.  When C++ constructs derived objects, it does so in phases. First, the most-base class(at the top of the INHERITANCE TREE) is constructed. Then each child class is constructed in order until the most-child class is constructed last. 
So the first Constructor of class B will be called and then the constructor of class D will be called.

During the destruction EXACTLY reverse order is followed. That is destructor starts at the most-derived class and works its way down to base class.
So the first destructor of class D will be called and then the destructor of class B will be called.



Discussion

No Comment Found