Constructor in C++

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. 

 
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.

#include 
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

 
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

#include 
using namespace std;
class xyz { // create a class “xyz”		
    public:
        xyz() //	definition of constructor					
    {
        cout << "Inside constructor";
    }
};
int main() {
    xyz obj; // constructor calle	
}

Output

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.

#include 
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

 
inside constructor
 inside constructor
 inside constructor
 inside constructor
 inside constructor
 

 Constructor with arguments in C++

 #include 
 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

 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

#include 
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

 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.    

#include 
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

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

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.

#include 
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

 
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.

Categories C++