Skip to content
Related Articles

Related Articles

C++ | Virtual Functions | Question 10
  • Difficulty Level : Easy
  • Last Updated : 12 Oct, 2013




#include<iostream>
using namespace std;
class Base  {
public:
    Base()    { cout<<"Constructor: Base"<<endl; }
    virtual ~Base()   { cout<<"Destructor : Base"<<endl; }
};
class Derived: public Base {
public:
    Derived()   { cout<<"Constructor: Derived"<<endl; }
    ~Derived()  { cout<<"Destructor : Derived"<<endl; }
};
int main()  {
    Base *Var = new Derived();
    delete Var;
    return 0;
}


(A)

Constructor: Base
Constructor: Derived
Destructor : Derived
Destructor : Base

(B)

Constructor: Base
Constructor: Derived
Destructor : Base

(C)

Constructor: Base
Constructor: Derived
Destructor : Derived

(D)

Constructor: Derived
Destructor : Derived


Answer: (A)

Explanation: Since the destructor is vitrual, the derived class destructor is called which in turn calls base class destructor.

Quiz of this Question

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :