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.


C++ Default Constructor Example 1

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


C++ Default Constructor (Implicit) Example 2

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


Expected Output

Garbage value Output

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

Pin It on Pinterest

Share This