Constructor in C++

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. 

Example: Write a C++ program  to create a constructor .

Output

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

Output

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 .

Output

 Constructor with arguments

Output

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

Output

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.    

Output

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

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.

Output

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.