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

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

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

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

int main()
{
    C c;
    return 0;
}
Explanation
Class objects are created from the bottom up, first the base class, then the members, and then the derived class itself. They are being destroyed in the opposite order: first the derived class, members, and then the base class.

Follow CodeGalaxy

Mobile Beta

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