Multiple Constructors & Default Argument Constructors in C++
1. Multiple Constructors (Constructor Overloading) (In Simple Words)
In C++, you don’t have to stop at just one constructor. A class can have more than one constructor!
This is called Constructor Overloading.
As long as each constructor has a different list of parameters (different count, different types, or different order), the compiler automatically chooses the right one depending on how you create the object.
Complex c1;Complex() (0 arguments)Sets real = 0, imag = 0
Complex c2(5.5);Complex(float) (1 argument)Sets real = 5.5, imag = 5.5
Complex c3(4.0, 9.2);Complex(float, float) (2 arguments)Sets real = 4.0, imag = 9.2
2. Complete Code Example: Overloaded Constructors
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
// 1. Zero-Argument Constructor (Default)
Complex() {
real = 0.0f;
imag = 0.0f;
}
// 2. One-Argument Constructor
Complex(float val) {
real = val;
imag = val;
}
// 3. Two-Argument Constructor (Parameterized)
Complex(float r, float i) {
real = r;
imag = i;
}
void display() const {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1; // Runs Constructor 1 (0 arguments)
Complex c2(5.0f); // Runs Constructor 2 (1 argument)
Complex c3(3.5f, 7.8f); // Runs Constructor 3 (2 arguments)
cout << "c1 = "; c1.display(); // Prints: 0 + 0i
cout << "c2 = "; c2.display(); // Prints: 5 + 5i
cout << "c3 = "; c3.display(); // Prints: 3.5 + 7.8i
return 0;
}
3. Constructors with Default Arguments
Just like normal functions, you can also assign default values to constructor arguments.
By using default arguments, one single constructor can do the job of 3 or 4 separate constructors!
#include <iostream>
using namespace std;
class Box {
private:
int length, width, height;
public:
// SINGLE CONSTRUCTOR WITH DEFAULT ARGUMENTS!
Box(int l = 1, int w = 1, int h = 1) {
length = l;
width = w;
height = h;
}
int getVolume() const {
return length * width * height;
}
};
int main() {
Box b1; // All 3 defaults used: 1 x 1 x 1 -> Volume = 1
Box b2(10); // length = 10, w = 1, h = 1 -> Volume = 10
Box b3(10, 5); // length = 10, width = 5, h = 1 -> Volume = 50
Box b4(10, 5, 2); // All 3 given: 10 x 5 x 2 -> Volume = 100
cout << "Volume b1: " << b1.getVolume() << endl;
cout << "Volume b2: " << b2.getVolume() << endl;
cout << "Volume b3: " << b3.getVolume() << endl;
cout << "Volume b4: " << b4.getVolume() << endl;
return 0;
}
4. The Dangerous Ambiguity Error (Exam Favorite!)
Look at this tricky situation that often trips students up in exams:
class Sample {
public:
// Constructor 1: Takes NO arguments
Sample() {
cout << "Constructor 1" << endl;
}
// Constructor 2: Takes 1 argument with a DEFAULT value
Sample(int x = 0) {
cout << "Constructor 2" << endl;
}
};
int main() {
Sample s1; // 💥 COMPILER ERROR: Call of overloaded 'Sample()' is ambiguous!
return 0;
}
Why does this error happen?
When you write Sample s1;, the compiler gets totally confused:
- It could call
Sample()because 0 arguments were passed. - It could also call
Sample(int x = 0)by applying the default value0!
Because both choices are equally valid, the compiler doesn’t know which one you wanted and halts with an Ambiguity Error.
Exam Tip: Never write a 0-argument default constructor in the same class where another constructor has default values for all its parameters!
DAVV Exam Preparation Tips
Frequently Asked 5-Mark Questions:
- What is constructor overloading in C++? Give a short example.
Model Answer:- Define: Having multiple constructors in the same class with different parameters.
- Show a short
Complexclass with 0-arg, 1-arg, and 2-arg constructors.
- What causes an Ambiguity Error in overloaded constructors?
Model Answer:- Happens when a class has both a default constructor
Sample()and a constructor with default argumentsSample(int x = 0). - Creating
Sample s;leaves the compiler unable to decide which constructor to invoke.
- Happens when a class has both a default constructor
Frequently Asked 10-Mark Questions:
- Explain Constructor Overloading and Constructors with Default Arguments. How do default arguments reduce code? Write a complete program demonstrating both.
Model Answer:- Explain Constructor Overloading and how signatures are matched at compile time.
- Show how
Box(int l = 1, int w = 1, int h = 1)can handle 0, 1, 2, or 3 arguments, replacing 4 overloaded constructors. - Write out complete code with sample output.
- Explain the Ambiguity Error trap with code.