Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Default Constructor And Its Need in C++

1. Introduction

A Constructor of a class takes the responsibility of creating the object, allocating spaces for its members and initializing it. Simply, it constructs the object and hence got the name, Constructor. In this article, we will see why one may need to implement a Default Constructor. First, we will see how to create a Constructor.

  • A constructor is a function without any return type
  • A function name must exactly match the class name

The above two rules qualify a class member function as Constructor.

2. Constructor with Single Parameter

The below code snippet shows a simple example for a Constructor. Here, we have a Constructor with a single parameter and it sets the data member with a default value.

1) The class

CCircle

  has a Constructor which takes an integer as a parameter. We use this parameter to initialize its members. The code is below:

2) In the Main function, when we are creating the object

obj

, we pass the value of 10. This will construct the object and initialize the value of the data member with the value of 10. The code snippet 04 prints the radius as 10. After this, we change the radius to 12 using the set function and printed the radius again. The code and its output are shown below:

Output of Default Constructor Example

Output of Default Constructor Example

What happens when we try to create the object passing no value as shown below?

The Compiler will not support the creation of object as shown above. Because, we have only one constructor which requires a parameter. In addition, our current class does not support creating the array of objects like

CCircle obj[10]

 . The below picture shows the Compiler Error.

Compiler Error: C2512

Compiler Error: C2512

3. Default Constructor in C++

Now, we know about the Constructor. When a Constructor takes no input parameter, then we call that as Default Constructor. The compiler will provide a Default Constructor when the class has no Constructor. Let us remove the code snippet ‘//Sample 02’ and change the main method as shown below:

Note that in Code snippet Sample 04, constructing object without parameter is allowed. The compiler is not giving error this time. Since we have the Compiler-Provided Default Constructor, the object creation succeeded. Also, be aware that we can create an array of objects as well. But what is the problem here? You should look at the below output now:

Compiler Provided Constructor Output

Compiler Provided Constructor Output

One can easily see the problem here. In the output, the first line shows a junk value for the radius. This is because, the compiler-supplied constructor allocated space for the data member without setting an initial value in it. How do we overcome this situation? We can provide our own default constructor as shown below:

Also, we can provide the one parameter constructor as shown below:

Below is the completed code and its output.

Output

Default Constructor Example: Final Output

Default Constructor Example: Final Output

Categories: C++

Tags: , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.