What is constructor overloading in C++?
A
Using a constructor inside another constructor
B
Defining multiple constructors with different parameter lists
C
Defining multiple classes with the same constructor
D
Using one constructor for multiple classes
Analysis & Theory
Constructor overloading means having multiple constructors in a class with different parameter lists.
Which of the following is an example of constructor overloading?
A
MyClass() and MyClass()
B
MyClass(int a) and MyClass(int b)
C
MyClass() and MyClass(int x)
D
MyClass(int a, int b) and MyClass(int a, int b)
Analysis & Theory
Overloading requires different parameter lists. MyClass() and MyClass(int x) are different.
What happens if two constructors in a class have the same number and type of parameters?
A
Compiler uses the first one only
B
It's allowed in overloading
C
It results in a compilation error
D
Both are called simultaneously
Analysis & Theory
Constructor overloading requires different signatures. Identical constructors cause a compilation error.
Which of the following is true about overloaded constructors?
A
They must all be public
B
They must return a value
C
They must differ in type or number of parameters
D
They must all call the default constructor
Analysis & Theory
Overloaded constructors must differ in the number or type of parameters.
Can constructor overloading be used to initialize an object in different ways?
A
No, only one constructor is allowed
B
Yes, by using different sets of parameters
C
Only with default arguments
D
Only in abstract classes
Analysis & Theory
Constructor overloading allows different ways to initialize an object using different arguments.
Which constructor will be called in the statement `MyClass obj(5.0);`?
Analysis & Theory
The constructor with a `double` parameter will be called for the `5.0` literal.
If no matching overloaded constructor is found, what happens?
A
The default constructor is used
B
Compiler calls all constructors
C
Compilation error occurs
D
Object is initialized with zeros
Analysis & Theory
If no matching constructor is found, the compiler throws a compilation error.
Is it possible to overload constructors with default arguments?
A
Yes, but all arguments must be default
B
No, default arguments are not allowed
C
Yes, but it may lead to ambiguity
D
Only if using inline constructors
Analysis & Theory
Overloading with default arguments is allowed, but can lead to ambiguity if not used carefully.
Which of the following can distinguish two overloaded constructors?
Analysis & Theory
All of these factors (type, order, number) can distinguish overloaded constructors.
Why is constructor overloading useful?
A
It helps initialize objects differently
B
It avoids writing multiple classes
C
It increases program size
D
It replaces function overloading
Analysis & Theory
Constructor overloading allows objects to be initialized in multiple ways, enhancing flexibility.