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

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

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

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

int main()
  C* p = new C();
  delete p;
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.

But the destructors are not declared virtual. Does not that mean that only ~C() will be called?

2023 Aug 17, 10:10:08 AM

#include <iostream>using namespace std; as single line will make cout not compile

2022 Aug 23, 2:23:34 PM

Follow CodeGalaxy

Mobile Beta

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