C++ Constructor Overloading
Constructor overloading in C++ allows a class to have multiple constructors with different parameter lists. This means you can define multiple constructors with the same name (the class name), but with different numbers or types of parameters. The appropriate constructor is called based on the arguments provided at the time of object creation.
Constructor overloading enhances the flexibility of class initialization by allowing different ways to instantiate objects with different initial values.
Constructor overloading refers to the ability to define more than one constructor in a class, each having a different set of parameters. C++ allows you to create multiple constructors with the same name as the class, as long as the parameters differ (in type or number).
Constructor overloading provides flexibility in object initialization. It allows you to create objects with different initial states by choosing the appropriate constructor for the situation.
For example, you may want to create a class that can be instantiated with:
In C++, constructor overloading works by defining multiple constructors in the same class with different parameter lists.
class ClassName {
public:
ClassName() { // Default constructor
// Initialization code
}
ClassName(int param1) { // Parameterized constructor with 1 argument
// Initialization code
}
ClassName(int param1, string param2) { // Parameterized constructor with 2 arguments
// Initialization code
}
};
Let’s consider an example where we have a class Rectangle
with multiple constructors, each providing a different way to initialize the object.
Rectangle
Class
#include <iostream>
using namespace std;
class Rectangle {
private:
int length;
int width;
public:
// Default constructor (no parameters)
Rectangle() {
length = 0;
width = 0;
cout << "Default constructor called!" << endl;
}
// Parameterized constructor with one argument
Rectangle(int l) {
length = l;
width = l; // Square shape (length = width)
cout << "Constructor with one argument called!" << endl;
}
// Parameterized constructor with two arguments
Rectangle(int l, int w) {
length = l;
width = w;
cout << "Constructor with two arguments called!" << endl;
}
// Member function to display area of the rectangle
void displayArea() {
cout << "Area of the rectangle: " << length * width << endl;
}
};
int main() {
// Create object using default constructor
Rectangle rect1;
rect1.displayArea(); // Output: 0
// Create object using constructor with one parameter (Square)
Rectangle rect2(5);
rect2.displayArea(); // Output: 25
// Create object using constructor with two parameters
Rectangle rect3(5, 10);
rect3.displayArea(); // Output: 50
return 0;
}
Output:
Default constructor called!
Area of the rectangle: 0
Constructor with one argument called!
Area of the rectangle: 25
Constructor with two arguments called!
Area of the rectangle: 50
Explanation:
length
and width
to 0
.length
and width
to the same value, effectively creating a square.length
and width
with the specified values, creating a rectangle.In addition to defining multiple constructors, you can also use default parameters in a single constructor to simulate constructor overloading. Default parameters are values that are automatically assigned if no value is passed.
#include <iostream>
using namespace std;
class Circle {
private:
float radius;
public:
// Constructor with default parameter
Circle(float r = 1.0) { // Default radius is 1.0
radius = r;
cout << "Constructor called with radius: " << radius << endl;
}
void displayArea() {
cout << "Area of the circle: " << 3.14159 * radius * radius << endl;
}
};
int main() {
// Create object using default parameter
Circle circle1;
circle1.displayArea(); // Output: 3.14159
// Create object using custom parameter
Circle circle2(5.0);
circle2.displayArea(); // Output: 78.53975
return 0;
}
Output:
Constructor called with radius: 1
Area of the circle: 3.14159
Constructor called with radius: 5
Area of the circle: 78.53975
Explanation:
r = 1.0
. If no argument is passed, r
is set to 1.0
by default.r
is provided, that value is used to initialize the radius
.Here are some important rules and guidelines to keep in mind when overloading constructors in C++: