Constructors and Destructors

Q1:
What will be the output of the following program?
#include<iostream.h>
class AptitudeBase
{   
    public:
    AptitudeBase()
    {
        cout<< 'Base OK. '; 
    }
};
class AptitudeDerived: public AptitudeBase
{
    public:
    AptitudeDerived()
    { 
        cout<< 'Derived OK. '; 
    }
    ~AptitudeDerived()
    { 
        cout<< 'Derived DEL. '; 
    }
};
int main()
{
    AptitudeBase    objB;
    AptitudeDerived objD;
    objD.~AptitudeDerived();
    return 0;
}

A Base OK. Derived OK. Derived DEL.

B Base OK. Base OK. Derived OK. Derived DEL.

C Base OK. Derived OK. Derived DEL. Derived DEL.

D Base OK. Base OK. Derived OK. Derived DEL. Derived DEL.

E The program will report compile time error.

ANS:D - Base OK. Base OK. Derived OK. Derived DEL. Derived DEL.

In oops,when a constructor of derived class is called compiler automatically invokes base class's default constructor (However in java we can use super(a,b) to specify parameterized constructor).

Therefore the virtually the definition of derived class constructor becomes:

AptitudeDerived()
{
AptitudeBase();//Line added By Compiler internally
cout<< "Derived OK. ";
}  



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.