Assume that we have constructor function for both Base and Derived classes. Now consider the declaration :
main ( )
Base *p = new Derived;
In what sequence, the constructor will be executed ?
UGC NET - 2013,
Answer: B
With Base *p = new Derived;, the Base class constructor runs first and then the Derived class constructor. A derived object contains a base part, and the base part has to be built before the derived part can use it.
The destructors run in the opposite order. The options saying that Derived runs first, or that one constructor is never called, are wrong, because creating the object with new Derived calls both constructors.
Please log in or register to report this question.