Constructor overloading in C++ will be covered in this tutorial with the help of some examples. It is very important to understand the concept of constructors first in order to fully grasp constructor overloading. Constructors are one-of-a-kind methods that are automatically called whenever we construct a class object. Initializing the data members of a new object is the main goal of the constructor. In a similar manner to function overloading, constructors can also be overloaded.
The class name is the same for overloaded constructors, but they take a different amount of arguments. The appropriate constructors are called depending on how many and what types of arguments are passed. You will learn everything about
As long as each constructor in C++ has a unique list of arguments, we are allowed to have many constructors in a class with the same name. This idea is referred to as constructor overloading.
Every constructor shares the same name as the class, but they vary depending on the number of arguments, the datatypes of the arguments, or a combination of the two. When using constructor overloading in C++, we can have multiple constructors in the same class as long as they each take a different set of parameters and It is very much similar to function overloading.
Constructor overloading is the concept of having numerous constructors with distinct parameters in a single class. The essential point to remember here is that constructors will execute according to the arguments. For example, if a program contains three constructors with 0 arguments, one argument, and two arguments, So, if we supply one argument to the constructor, the compiler will run the constructor that takes one argument.
#include <iostream>
using namespace std;
class Complex
{
int a, b;
public:
Complex(){
a = 0;
b =0;
}
Complex(int x, int y)
{
a = x;
b = y;
}
Complex(int x){
a = x;
b = 0;
}
void printNumber()
{
cout << " So your number is " << a << " + " << b << "i" << endl;
}
};
int main()
{
Complex c1(8, 10);
c1.printNumber();
Complex c2(9);
c2.printNumber();
Complex c3;
c3.printNumber();
return 0;
}
Output:
So your number is 8 + 10i So your number is 9 + 0i So your number is 0 + 0i
Explanation
Function overloading is used to make programs easier to read. It is not required to use separate names for the same action when using function overloading. Function overloading in CPP is accomplished by calling multiple functions with the same name but different parameters.
Operator overloading is used to perform many operations on the same operand. Operator overloading involves performing operations on user-defined data types to increase the number of CPP operators.
The advantage of a constructor overloading in C++ is that it allows for the creation of several types of objects belonging to a class by having additional constructors in a class. In fact, it is comparable to compile-time polymorphism, which is another name for C++ function overloading. Because of this, C++ needs constructor overloading if we want to construct an object in a different way.