0
0
C++programming~10 mins

Constructor calling order in C++ - Interactive Code Practice

Choose your learning style9 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.