0
0
C++programming~20 mins

Constructor overloading in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Constructor Overloading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of overloaded constructors with default and parameterized calls

What is the output of this C++ program that uses constructor overloading?

C++
#include <iostream>
using namespace std;

class Box {
public:
    int length, width;
    Box() { length = 0; width = 0; cout << "Default constructor called\n"; }
    Box(int l) { length = l; width = 0; cout << "One-parameter constructor called\n"; }
    Box(int l, int w) { length = l; width = w; cout << "Two-parameter constructor called\n"; }
};

int main() {
    Box b1;
    Box b2(5);
    Box b3(3, 4);
    return 0;
}
A
Two-parameter constructor called
One-parameter constructor called
Default constructor called
B
Default constructor called
One-parameter constructor called
Two-parameter constructor called
C
One-parameter constructor called
Default constructor called
Two-parameter constructor called
D
Default constructor called
Two-parameter constructor called
One-parameter constructor called
Attempts:
2 left
πŸ’‘ Hint

Look at which constructor matches each object creation.

❓ Predict Output
intermediate
2:00remaining
Output when calling overloaded constructors with explicit casts

What will this C++ program print when creating objects with explicit casts?

C++
#include <iostream>
using namespace std;

class Point {
public:
    int x, y;
    Point() { x = 0; y = 0; cout << "Default Point\n"; }
    Point(int a) { x = a; y = 0; cout << "One-arg Point\n"; }
    Point(int a, int b) { x = a; y = b; cout << "Two-arg Point\n"; }
};

int main() {
    Point p1 = Point();
    Point p2 = Point(10);
    Point p3 = Point(7, 8);
    return 0;
}
A
Default Point
One-arg Point
Two-arg Point
B
One-arg Point
Default Point
Two-arg Point
C
Two-arg Point
One-arg Point
Default Point
D
Default Point
Two-arg Point
One-arg Point
Attempts:
2 left
πŸ’‘ Hint

Explicit cast calls the matching constructor.

πŸ”§ Debug
advanced
2:00remaining
Identify the error in overloaded constructors with default arguments

What error does this C++ code produce?

C++
#include <iostream>
using namespace std;

class Sample {
public:
    int a, b;
    Sample(int x = 0, int y = 0) { a = x; b = y; cout << "Constructor 1\n"; }
    Sample(int x) { a = x; b = 10; cout << "Constructor 2\n"; }
};

int main() {
    Sample s1;
    Sample s2(5);
    return 0;
}
ACompilation error: redefinition of constructor
BRuntime error: segmentation fault
C
No error, prints:
Constructor 1
Constructor 2
DCompilation error: ambiguous call to constructor
Attempts:
2 left
πŸ’‘ Hint

Check how default arguments affect constructor overloading.

πŸ“ Syntax
advanced
2:00remaining
Which constructor declaration is invalid in C++?

Which of the following constructor declarations will cause a syntax error in C++?

AMyClass(int x = 5, int y);
BMyClass(int x, int y = 10);
CMyClass();
DMyClass(const MyClass &obj);
Attempts:
2 left
πŸ’‘ Hint

Default arguments must be trailing parameters.

πŸš€ Application
expert
2:00remaining
Number of objects created and constructors called

Consider this C++ program. How many objects are created and how many constructor calls are made in total?

C++
#include <iostream>
using namespace std;

class Test {
public:
    Test() { cout << "Default\n"; }
    Test(int x) { cout << "Param\n"; }
};

int main() {
    Test t1;
    Test t2(10);
    Test t3 = t2;
    Test t4 = Test(5);
    return 0;
}
A4 objects created, 4 constructor calls
B3 objects created, 4 constructor calls
C4 objects created, 5 constructor calls
D3 objects created, 3 constructor calls
Attempts:
2 left
πŸ’‘ Hint

Remember copy initialization calls copy constructor if defined, else default copy constructor is used.