Bird
Raised Fist0
C++programming~10 mins

Constructor calling order in C++ - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to call the base class constructor from the derived class.

C++
class Base {
public:
    Base() { std::cout << "Base constructor\n"; }
};

class Derived : public Base {
public:
    Derived() : [1]() { std::cout << "Derived constructor\n"; }
};

int main() {
    Derived d;
    return 0;
}
Drag options to blanks, or click blank then click option'
ADerived
BBase
Cthis
Dsuper
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'Derived()' instead of 'Base()' in the initializer list.
Trying to call 'super()' which is not valid in C++.
2fill in blank
medium

Complete the code to call the parameterized base class constructor from the derived class.

C++
class Base {
public:
    Base(int x) { std::cout << "Base constructor with " << x << "\n"; }
};

class Derived : public Base {
public:
    Derived(int y) : [1](y) { std::cout << "Derived constructor with " << y << "\n"; }
};

int main() {
    Derived d(5);
    return 0;
}
Drag options to blanks, or click blank then click option'
ABase
Bthis
CDerived
Dsuper
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Calling 'Derived(y)' instead of 'Base(y)' in the initializer list.
Trying to call 'super(y)' which is invalid in C++.
3fill in blank
hard

Fix the error in the constructor calling order by completing the code.

C++
class A {
public:
    A() { std::cout << "A constructor\n"; }
};

class B : public A {
public:
    B() { std::cout << "B constructor\n"; }
};

class C : public B {
public:
    C() : [1]() { std::cout << "C constructor\n"; }
};

int main() {
    C c;
    return 0;
}
Drag options to blanks, or click blank then click option'
AB
BA
CC
Dsuper
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Calling A() directly from C's constructor, which is not the immediate base class.
Trying to call 'super()' which is not valid in C++.
4fill in blank
hard

Fill both blanks to create a constructor in Derived that calls the Base constructor with an argument and initializes member x.

C++
class Base {
public:
    Base(int a) { std::cout << "Base constructor with " << a << "\n"; }
};

class Derived : public Base {
    int x;
public:
    Derived(int a, int b) : [1](a), [2](b) { std::cout << "Derived constructor with " << b << "\n"; }
};

int main() {
    Derived d(10, 20);
    return 0;
}
Drag options to blanks, or click blank then click option'
ABase
Bx
Cy
DDerived
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'Derived' instead of 'Base' to call the base constructor.
Initializing a non-existent member 'y' instead of 'x'.
5fill in blank
hard

Fill the blanks to complete the constructor calling order in multiple inheritance.

C++
class A {
public:
    A() { std::cout << "A constructor\n"; }
};

class B {
public:
    B() { std::cout << "B constructor\n"; }
};

class C : public A, public B {
public:
    C() : [1](), [2]() { std::cout << "C constructor\n"; }
};

int main() {
    C c;
    return 0;
}
Drag options to blanks, or click blank then click option'
AA
BB
CA::A
DB::B
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using scoped names like A::A() instead of just A().
Reversing the order of base constructors.

Practice

(1/5)
1. In C++, when creating an object of a derived class, which constructor is called first?
easy
A. Member objects' constructors
B. Derived class constructor
C. Base class constructor
D. Destructor of the base class

Solution

  1. Step 1: Understand constructor order in inheritance

    When an object of a derived class is created, the base class constructor runs first to initialize the base part.
  2. Step 2: Identify the first constructor called

    After the base class constructor, member objects and then the derived class constructor run.
  3. Final Answer:

    Base class constructor -> Option C
  4. Quick Check:

    Base constructor runs first [OK]
Hint: Base constructor always runs before derived [OK]
Common Mistakes:
  • Thinking derived constructor runs first
  • Confusing member object construction order
  • Assuming destructors run during construction
2. Which of the following is the correct syntax to call a base class constructor from a derived class constructor in C++?
easy
A. Derived() { Base(); }
B. Derived() : Base {}
C. Derived() -> Base() {}
D. Derived() : Base() {}

Solution

  1. Step 1: Recall base constructor call syntax

    In C++, the base class constructor is called in the initializer list of the derived constructor using a colon and parentheses.
  2. Step 2: Match correct syntax

    Derived() : Base() {} uses the correct syntax: Derived() : Base() {}. Others are invalid or incorrect.
  3. Final Answer:

    Derived() : Base() {} -> Option D
  4. Quick Check:

    Use colon and parentheses for base constructor call [OK]
Hint: Use colon and parentheses to call base constructor [OK]
Common Mistakes:
  • Calling base constructor inside derived constructor body
  • Using arrow (->) instead of colon
  • Omitting parentheses after base class name
3. Consider the following code:
class A {
public:
  A() { std::cout << "A "; }
};
class B {
public:
  B() { std::cout << "B "; }
};
class C : public A {
  B b;
public:
  C() { std::cout << "C "; }
};

int main() {
  C obj;
  return 0;
}

What is the output when this program runs?
medium
A. A B C
B. B A C
C. C A B
D. A C B

Solution

  1. Step 1: Determine constructor call order

    First, base class A constructor runs printing "A ". Then member object b of class B is constructed printing "B ". Finally, derived class C constructor runs printing "C ".
  2. Step 2: Combine outputs in order

    The output is "A B C " exactly in that sequence.
  3. Final Answer:

    A B C -> Option A
  4. Quick Check:

    Base, members, derived = A B C [OK]
Hint: Base then members then derived constructor order [OK]
Common Mistakes:
  • Assuming members construct before base
  • Mixing order of output strings
  • Ignoring member object construction
4. Given this code snippet:
class Base {
public:
  Base() { std::cout << "Base "; }
};
class Derived : public Base {
public:
  Derived() { std::cout << "Derived "; }
};

int main() {
  Derived d();
  return 0;
}

What is the output when this program runs?
medium
A. No output
B. Derived Base
C. Base Derived
D. Compilation error

Solution

  1. Step 1: Analyze object declaration

    The line 'Derived d();' is a function declaration, not an object instantiation, so no constructor runs.
  2. Step 2: Understand program output

    Since no object is created, no constructors run and no output is printed.
  3. Final Answer:

    No output -> Option A
  4. Quick Check:

    Function declaration, no object created [OK]
Hint: Watch out for most vexing parse: 'Derived d();' is a function [OK]
Common Mistakes:
  • Thinking 'Derived d();' creates an object
  • Expecting constructor output
  • Ignoring C++ parsing rules
5. You have a class hierarchy:
class X {
public:
  X() { std::cout << "X "; }
};
class Y {
public:
  Y() { std::cout << "Y "; }
};
class Z : public X {
  Y y1, y2;
public:
  Z() { std::cout << "Z "; }
};

int main() {
  Z obj;
  return 0;
}

What is the exact output when this program runs?
hard
A. Y Y X Z
B. X Y Y Z
C. X Z Y Y
D. Y X Y Z

Solution

  1. Step 1: Identify constructor call order

    First, base class X constructor runs printing "X ". Then member objects y1 and y2 of class Y are constructed in declaration order, printing "Y " twice.
  2. Step 2: Derived class constructor runs last

    Finally, derived class Z constructor runs printing "Z ".
  3. Final Answer:

    X Y Y Z -> Option B
  4. Quick Check:

    Base, members in order, derived = X Y Y Z [OK]
Hint: Members construct in declaration order after base [OK]
Common Mistakes:
  • Mixing member construction order
  • Assuming derived runs before members
  • Ignoring multiple member objects
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard