The constructor is key for object initialization. The mechanism of the constructor is made considerably more powerful by combining with the feature of overloading. It is made possible by providing more than one constructor in a class. It is called constructor overloading.
Constructor Overloading in C++ allows a class to have more than one constructors that have the same name as that of the class but differs only in terms of a number of parameters or parameter’s datatype or both. Constructors often use this facility of constructor overloading to provide more than one style of initialization. By overloading a constructor of a class, we make the class more versatile as it allows you to-construct objects in a variety of ways. To understand the concept of constructor overloading, let us consider the following program.
#fnclude<iostream.h> #include<conio.h> class rectangle { private: int length,breadth; public: rectangle () { length = breadth = 0; cout<<"constructor with zero parameter called\n"; } rectangle (int a) { length = breadth = a; cout<<"constructor with one parameter called\n"; } rectangle(int a,int b) { length = a; breadth = b; cout<<"constructor with two parameters called\n"; } int area() { return(length*breadth); } }; int main() { clrscr(); rectangle r1; //constructor with zero parameter called rectangle r2(5); //constructor with one parameter called rectangle r3(7,8); //constructor with two parameters called cout<<"\nArea of first rectangle = "<<r1.area() ; cout<<"\nArea of square = "<<r2.area() ; cout<<"\nArea of second rectangle = "<<r3.area(): getch(); return 0; } Output : constructor with zero parameter called constructor with one parameter called constructor with two parameter called Area of first rectangle = 0 Area of square = 25 Area of second rectangle = 56
Explanation: In this program, the class rectangle contains three constructors with the same name rectangle, which differs only in the number of parameters and hence overloaded.
The first constructor rectangle() doesn’t take any parameter and initialize both data members to 0. The second constructor rectangle (int a) takes a parameter of int type and initialize both the data member’s length and breadth with a value of an (i.e., 5). The third constructor rectangle(int a, int b) takes two parameters and b of int types used to initialize the data member’s length and breadth, respectively.
Now we have defined three constructors associated with the class rectangle. These instructors will be executed when an object of rectangle class is created upon the number of arguments specified in the definition of an object.
On execution of the first statement rectangle r1; in main (), an object r1 of type rectangle is created, and constructor with no parameter is called as no arguments are specified after r1 object definition. It initializes both of its data members to a value 0 and displays the message constructor with a zero parameter called.
The statement rectangle r2(5); creates an object r2 of type rectangle and invokes a constructor with one parameter as only one argument is specified after r2 object definition. The data member’s length and breadth of object r2 are initialized with the value 5 passed to a single parameter constructor. It also displays the message constructor with one parameter called on the console indicating the type of constructor being called.
The statement, rectangle r3 (7, 8), creates an object r3 of type rectangle and invokes a constructor with two parameters as two arguments are specified after r3 object definition. This constructor initializes the data member’s length and breadth with values of parameters a and b (i.e., 7 and 8), respectively. It also displays the message constructor with two parameters called on the console indicating the type of constructor being called. Finally, these values are used to calculate the area of three rectangles resented by objects r1, r2 and r3.
We can specify the arguments to a constructor in the following three equivalent forms.
rectangle r2(5);//Implicit call rectangle r2 = rectangle(5) ;//Explicit call rectangle r2 = 5; //Implicit call
The first and the third forms are the implicit constructor call. These representations of passing an argument to a parameterized constructor are a short-hand for the second form of constructor call. In the second form, we are explicitly calling the constructor for initializing the object r2.
The third form can only be used when specifying a single argument after object definition, which is quite similar to basic variable initialization. However, only the first and second forms of the constructor can be used for two or more arguments. In general, we recommend using the first form as it is shorter and easy to implement. Some of the valid and invalid constructor calls are shown below :
rectangle r3 = rectangle(7,8);//Valid rectangle r3 = (7,8); //Invalid rectangle r1(); //Invalid