Constructor in C++ is similar to a function that has the same name as the class in which it resides.
Constructor is automatically called when the object of that class created in which it is resides.
A class can contain more than one constructor. Constructor does not have a return value.
Characteristics of constructor
Constructor has the same name as the class in which it resides.
Constructor is automatically called when the object of that class created in which it resides.
Constructors have neither return value nor void.
Constructors 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 | #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 a class with the name “xyz” and one constructor “xyz” with the same name as the class.
Constructor “xyz” is automatically called when the object “obj” of that class is created.
Example: Constructor definition inside the class
1 2 3 4 5 6 7 8 9 10 11 12 | #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 in C++
Constructor is automatically called when the object of that class created in which it is resides.
If we create multiple objects then multiple times constructor will call.
For example: in the below program we have created a total of 5 objects means five times the constructor will call.
Example: Write a C++ program to show each object is called separately to the 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() // 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 in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #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 C++
In C++ programming like a function, we can create more than one constructor with the same name as the class.
But each constructor must have a different parameter list.
Such constructors are called overloaded constructors and this procedure of creating more than one constructor with different parameters 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 | #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 a default argument.
Consider a following the following example:
Example: Write a program to declare a default argument in the constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #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 a default argument.
When an object (obj1) is created and the constructor is called without an argument then the default value of x & y (10 & 50 respectively ) will be used.
When an object (obj2) is created and the constructor is called with arguments (2 & 3) then values of x & y (2 & 3 respectively )will be used.
When an object (obj3) is created and the constructor is called with the argument(6) then the value of x is 6 and the default value of y ( 50 )will be used.
Copy Constructor
In C++ programming, Copy constructors are always used when the compiler has to create a temporary object of a class.
Whenever a constructor is called and we pass an object into a constructor, a temporary copy of the 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 the program, we have created a class “xyz” and constructor “xyz(xyz &) ”.
In a constructor, we are passing an object, and within a constructor parameter “xyz &” creates a temporary copy of the 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 | #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 constructors. When object A has created a constructor with no argument is called.
When object B is created the copy constructor is called & the object is passed and the data member is initialized.
When object C is created with the assignment with object A; this is also called copy constructor.
In the statement xyz C = A compiler copies all the data members of object A to object C.