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.

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

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты