What will be printed out as a result of the following code execution?
#include <iostream>using namespace std;

class A
{
public:
  A(){ cout << "A() "; }
  ~A(){ cout << "~A() "; }
};

class B : public A
{
public:
  B(){ cout << "B() "; }
  ~B(){ cout << "~B() "; }
};

class C : public B
{
public:
  C(){ cout << "C() "; }
  ~C(){ cout << "~C() "; }
};

int main()
{
  C* p = new C();
  delete p;
}
Explanation
When creating an object of a child class, constructors are called in order from the base to the child. When the object is destroyed, the destructors are called in the opposite order - from the destructor of the child class to the base destructor.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes