Parameterized Constructor In C++

Card image cap

Constructor?

Constructor is a special member function of a class that is executed whenever we create new objects of that class.


It is used to initialize the data members of new object generally.
The constructor in C++ has the same name as class or structure.

Constructor doesn’t have a return type.

Constructor is automatically called when we create the object of the class.

Member function needs to be called explicitly using object of class.


Card image cap

Parameterized Constructor

Constructors with parameters are known as Parameterized constructors. It is used to provide different values to distinct objects.

A default constructor does not have any parameter, but if you need, a constructor can have parameters. This helps you to assign initial value to an object at the time of its creation as shown in the following example.

Example :



Output :

 Object is being created, length = 10
 Length of line : 10
 Length of line : 6
      
Card image cap