Challenge - 5 Problems
Constructor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediateConstructor calling order in single inheritance
What is the output of this C++ program showing constructor calls in single inheritance?
C++
#include <iostream> using namespace std; class Base { public: Base() { cout << "Base constructor\n"; } }; class Derived : public Base { public: Derived() { cout << "Derived constructor\n"; } }; int main() { Derived d; return 0; }
Attempts:
2 left
π‘ Hint
Remember base class constructors run before derived class constructors.
β Incorrect
In C++, when an object of a derived class is created, the base class constructor runs first, then the derived class constructor.
β Predict Output
intermediateConstructor calling order in multiple inheritance
What is the output of this C++ program demonstrating constructor calls in multiple inheritance?
C++
#include <iostream> using namespace std; class A { public: A() { cout << "A constructor\n"; } }; class B { public: B() { cout << "B constructor\n"; } }; class C : public A, public B { public: C() { cout << "C constructor\n"; } }; int main() { C c; return 0; }
Attempts:
2 left
π‘ Hint
Base classes are constructed in the order they are declared in the class header.
β Incorrect
In multiple inheritance, base classes are constructed in the order they appear in the derived class declaration, then the derived class constructor runs.
β Predict Output
advancedConstructor calling order with virtual inheritance
What is the output of this C++ program illustrating constructor calls with virtual inheritance?
C++
#include <iostream> using namespace std; class Base { public: Base() { cout << "Base constructor\n"; } }; class A : virtual public Base { public: A() { cout << "A constructor\n"; } }; class B : virtual public Base { public: B() { cout << "B constructor\n"; } }; class Derived : public A, public B { public: Derived() { cout << "Derived constructor\n"; } }; int main() { Derived d; return 0; }
Attempts:
2 left
π‘ Hint
Virtual base class constructors are called before any derived class constructors.
β Incorrect
With virtual inheritance, the virtual base class constructor is called only once before any non-virtual base classes or derived class constructors.
β Predict Output
advancedConstructor calling order with member objects
What is the output of this C++ program showing constructor calls including member objects?
C++
#include <iostream> using namespace std; class Member { public: Member() { cout << "Member constructor\n"; } }; class Container { Member m; public: Container() { cout << "Container constructor\n"; } }; int main() { Container c; return 0; }
Attempts:
2 left
π‘ Hint
Member objects are constructed before the container class constructor body runs.
β Incorrect
When an object contains member objects, those members are constructed first, then the container's constructor runs.
π§ Conceptual
expertOrder of constructor calls in complex inheritance
Given this class hierarchy with virtual and non-virtual inheritance, which sequence correctly shows the constructor call order when creating an object of class D?
C++
class A { public: A() { cout << "A"; } }; class B : virtual public A { public: B() { cout << "B"; } }; class C : virtual public A { public: C() { cout << "C"; } }; class D : public B, public C { public: D() { cout << "D"; } };
Attempts:
2 left
π‘ Hint
Virtual base class A is constructed once before B and C.
β Incorrect
Virtual base A is constructed first, then B and C in declaration order, then D.
