Default Constructor

tutorial and example
tutorial and example

C++ Default Constructor

Default constructor is such constructor which does not take any parameter. Constructor initializes the data member when 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 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 value to data member.

Output:

Expected Output

Garbage value Output

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