Default Constructor

C++ Default Constructor

A default constructor is such constructor which does not take any parameter. A constructor initializes the data member when a class object created.

If a programmer does not create any constructor explicitly, then C++ compiler implicitly creates a default constructor and initialize data member with its default value.

Syntax:

C++ Default Constructor Example 1

Creating a class with the name ‘Employee‘ and a default constructor ‘Employee()‘ which initialize data member ‘id‘.

Output:

C++ Default Constructor (Implicit) Example 2

In this example compiler itself internally (implicit) create default constructor and initialize the value to a data member.

Output:

Expected Output

Garbage value Output

Some time default implicit constructor initialize garbage value to a data member.

Leave a Comment