Default Constructor In C++

Card image cap

Default Constructor?

Default constructor is the constructor which doesn’t take any argument. It has no parameters.


In practical programs, we often need to initialize the various data elements of the different object with different values when they are created. This can be achieved by passing the arguments to the constructor functions when the object is created.

Note: Even if we do not define any constructor explicitly, the compiler will automatically provide a default constructor implicitly.

Syntax:

Card image cap

Example 1:



Output :

 Roll : 1
 Name : Kumar
 Marks : 78.42
Card image cap

Example 2:



Output :

    Method 1: 
    Value after initialization : 
    X: 0
    Y: 0
    Enter Value of X: 23
    Enter Value of Y: 24
    Value after User Input : 
    X: 23
    Y: 24

    Method 2: 
    Value after initialization : 
    X: 0
    Y: 0
Card image cap