What is the output of this C++ program that uses constructor overloading?
#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; }
Look at which constructor matches each object creation.
The object b1 calls the default constructor with no parameters.
The object b2 calls the constructor with one integer parameter.
The object b3 calls the constructor with two integer parameters.
Each constructor prints a unique message.
What will this C++ program print when creating objects with explicit casts?
#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; }
Explicit cast calls the matching constructor.
Each object is created by calling the constructor that matches the arguments:Point() calls default constructor.Point(10) calls one-argument constructor.Point(7, 8) calls two-argument constructor.
What error does this C++ code produce?
#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; }
Check how default arguments affect constructor overloading.
The constructor Sample(int x = 0, int y = 0) can be called with zero or one argument.
The constructor Sample(int x) also takes one argument.
This causes ambiguity when calling Sample(5) because both constructors match.
The compiler cannot decide which one to use, so it raises an ambiguous call error.
Which of the following constructor declarations will cause a syntax error in C++?
Default arguments must be trailing parameters.
In option A, the parameter with a default value int x = 5 comes before a parameter without a default value int y.
This is invalid in C++ because once a parameter has a default value, all parameters to its right must also have default values.
Options B, C, and D are valid constructor declarations.
Consider this C++ program. How many objects are created and how many constructor calls are made in total?
#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; }
Remember copy initialization calls copy constructor if defined, else default copy constructor is used.
Objects created: t1, t2, t3, t4 β 4 objects.
Constructor calls:
- t1 calls default constructor.
- t2 calls parameterized constructor.
- t3 is copy initialized from t2, calls copy constructor (compiler generated).
- t4 is initialized by temporary object Test(5) (parameterized constructor), then copy initialized to t4 (copy constructor).
Total constructor calls: 5 (default, param, copy, param, copy).