Constructor is similar to function that has a same name as the class in which it is resides .
Constructor is automatically called when the object of that class created in which it is resides.
A class can contains more than one constructor. Constructor does not have a return value.
Characteristics of constructor
Constructor has a same name as the class in which it is resides.
Constructor is automatically called when the object of that class created in which it is resides.
Constructor have neither return value nor void .
Constructor can be overloaded .
The constructor without argument is called as default constructor.
1 2 3 4 5 6 7 | Syntax: class xyz { // create a class xyz public: xyz(); // declaration of constructor void show(); // declaration of member function. }; xyz::xyz() // definition of constructor { } void xyz::show() // definition of member function { } int main() { xyz obj; // create a object “obj” & constructer called } |
Example: Write a C++ program to create a constructor .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <iostream> using namespace std; class xyz { // create a class “xyz” public: xyz(); // declaration of constructor with class name “xyz” }; xyz::xyz() // definition of constructor { cout << "Inside constructor "; } int main() { xyz obj; // constructor called } |
Output
1 | Inside constructor |
Note: In the above program, we have created class with name “xyz” and one constructor “xyz” with same name as the class.
Constructor “xyz” is automatically called when the object “obj”of that class created.
Example: Constructor definition inside the class
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <iostream> using namespace std; class xyz { // create a class “xyz” public: xyz() // definition of constructor { cout << "Inside constructor"; } }; int main() { xyz obj; // constructor calle } |
Output
1 | Inside constructor |
Constructor Calling
Constructor is automatically called when the object of that class created in which it is resides.
If we create multiple object then multiple times constructor will call.
For example: in the below program we have created total 5 object means five times constructor will call.
Example: Write a C++ program to show each object is called separately to constructor .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> using namespace std; class xyz { // create a class “xyz” public: xyz() // definition of constructor { cout << "\n inside constructor"; } }; int main() { xyz obj1, obj2, obj3; // constructor called (3 times) xyz a[2]; // constructor called (2 times) } |
Output
1 2 3 4 5 | inside constructor inside constructor inside constructor inside constructor inside constructor |
Constructor with arguments
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> using namespace std; class xyz { // create a class “xyz” public: xyz(int a, int b, int c)// definition of constructor { cout << "Value of a, b & c = " << a <<" "<< b<<" " << c; } }; int main() { xyz obj(10, 20, 30); // constructor called } |
Output
1 | Value of a, b & c = 10 20 30 |
Constructor Overloading
In a C++ programming like a function, we can create a more than one constructor with same name as the class.
But each constructor must has a different parameter list.
Such constructor are called overloaded constructor and this procedure of creating a more than one constructor with different parameter is known as Constructor overloading.
Example of constructor overloading
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> using namespace std; class xyz { public: xyz() // overloaded constructor { cout << "Constructor with no argument " << endl; } xyz(int a) // overloaded constructor { cout << " a = " << a << endl; } xyz(char c) // overloaded constructor { cout << " c = " << c; } }; int main() { xyz obj1; // constructor called xyz obj2(10); xyz obj3('s'); } |
Output
1 2 3 | Constructor with no argument a = 10 c = s |
Constructor with default argument
In a C++ language like a function, it is possible to declare a constructor with default argument.
Consider a following a following example:
Example: WAP to declare a default argument in constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> using namespace std; class xyz { public: xyz(int x = 10, int y = 50); // declaration of constructor with default argument }; xyz::xyz(int x, int y) // definition of constructor { cout << "x = " << x << " y = " << y << endl; } int main() { xyz obj1; // constructor called xyz obj2(2, 3); xyz obj3(6); } |
Output
1 2 3 | x = 10 y = 50 x = 2 y = 3 x = 6 y = 50 |
Note: In the above program, we have created one constructor with default argument .
When object (obj1) is created and constructor are called without argument then default value of x & y (10 & 50 respectively ) will be used.
When object (obj2) is created and constructor are called with argument(2 & 3) then value of x & y (2 & 3 respectively )will be used.
When object (obj3) is created and constructor are called with argument(6) then value of x is 6 and default value of y ( 50 )will be used.
Copy Constructor
In a C++ programming, Copy constructor are always used when the compiler has to create a temporary object of a class.
Whenever constructor is called and we pass an object into a constructor, a temporary copy of object is created.
This is called copy constructor.
Copy constructor
1 2 3 4 5 6 | class xyz // create a class “xyz” { public: xyz(xyz & ); // copy constructor }; |
In the above part of program, we have created a class “xyz” and constructor “xyz(xyz &) ”.
In a constructor we are passing object and within a constructor parameter “xyz &” creating a temporary copy of object . This is called copy constructor.
Example: Write a C++ program to show the use of copy constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> using namespace std; class xyz { public: int x; xyz() // constructor without argument { x = 10; } xyz(xyz & o) // copy constructor { x = o.x; } }; int main() { xyz A; // create object “A” & constructor called xyz B(A); // create object “B” constructor called & pass object “A” cout << A.x << endl; cout << B.x << endl; xyz C = A; // copy constructor is executed cout << C.x << endl; } |
Output
1 2 3 | 10 10 10 |
Note: In this program, class xyz contains one data member x and two constructor. When object A is created constructor with no argument is call.
When object B is created the copy constructor is call & object is passed and data member is initialize.
When the object C is created with assignment with object A; this is also call copy constructor.
In the statement xyz C = A compiler copies all the data members of object A to object C.