Multiple constructor: Multiple constructors is a category of constructor in which a class can have more than one constructor. This is also known as constructor overloading. All constructors are defined with the same name as the class name they belong to. Each of the constructors contains different number of arguments. Depending upon the number of arguments and their data type, the compiler executes appropriate constructor.
Example:-
#include<iostream.h>
#include<conio.h>
class integer
{
int m, n;
public:
integer()
{
m = 0;
n = 0;
}// constructor 1
integer(int a, int b)
{
m = a;
n = b;
cout<<"value of m="<<a;
cout<<"value of n="<<b;
} // constructor 2
};
void main()
{
clrscr();
integer i1;
integer i2(20,40);
getch();
}
In the above example, two constructors are defined and invoked; this is referred as multiple constructors.
The first constructor does not accept any argument and the second accepts two integer arguments.
In void main( ):
integer i1; - This statement invokes first constructor.
integer i2 (20, 40); -This statement invokes second constructor.