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. 

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

Output

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

Output

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.

Output

 Constructor with arguments in C++

Output

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

Output

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.    

Output

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

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.

Output

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++