0
0
C++programming~10 mins

Constructor overloading 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 declare a constructor that takes no arguments.

C++
class Box {
public:
    Box() [1] {}
};
Drag options to blanks, or click blank then click option'
Areturn
Bvoid
C: width(0), height(0), depth(0)
Dint
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Trying to put a return type for the constructor.
Using 'void' keyword in constructor declaration.
2fill in blank
medium

Complete the code to declare a constructor that takes three integer parameters.

C++
class Box {
public:
    Box(int w, int h, int d) [1] {}
};
Drag options to blanks, or click blank then click option'
Areturn w, h, d;
Bwidth = w; height = h; depth = d;
Cvoid
Dint
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Trying to use 'return' inside constructor.
Adding a return type like 'void' or 'int' to constructor.
3fill in blank
hard

Fix the error in the constructor declaration to properly overload it.

C++
class Box {
public:
    Box() {}
    Box(int w, int h, int d) [1] {}
};
Drag options to blanks, or click blank then click option'
ABox()
BBox(int w, int h)
Cvoid Box(int w, int h, int d)
DBox(int w, int h, int d)
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Adding a return type like 'void' to constructor.
Changing the constructor name to something else.
4fill in blank
hard

Fill both blanks to complete the constructor initializer list and body.

C++
class Box {
    int width, height, depth;
public:
    Box(int w, int h, int d) [1] [2] {}
};
Drag options to blanks, or click blank then click option'
A: width(w), height(h), depth(d)
Bwidth = w; height = h; depth = d;
C{}
D;
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Putting assignments inside the initializer list.
Forgetting the colon before initializer list.
5fill in blank
hard

Fill all three blanks to complete the overloaded constructors with default values.

C++
class Box {
    int width, height, depth;
public:
    Box() [1] {}
    Box(int w) [2] {}
    Box(int w, int h, int d) [3] {}
};
Drag options to blanks, or click blank then click option'
A: width(0), height(0), depth(0)
B: width(w), height(0), depth(0)
C: width(w), height(h), depth(d)
Dwidth = 0; height = 0; depth = 0;
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Trying to assign values inside the constructor body instead of initializer list.
Forgetting to initialize all members.