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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include "stdafx.h" #include <iostream> using namespace std; class CCircle { private: //Sample 01: Only one member for Demo purpose int m_radius; public: //Sample 02: Constructor CCircle(int rad) { m_radius = rad; } //Sample 03: Get and Set methods void SetRadius(int rad) { m_radius = rad; } int GetRadius() { return m_radius; } }; int main() { //Sample 04: Create the Circle Object & Display Radius CCircle obj(10); cout<< "Radius: " << obj.GetRadius() << endl; //Sample 05: Set the radius and Display the Radius obj.SetRadius(12); cout<< "Radius: " << obj.GetRadius() << endl; //Sample 06: Here is the need for Default Constructor //CCircle obj2; } |
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:
1 2 3 4 5 |
//Sample 02: Constructor CCircle(int rad) { m_radius = rad; } |
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:
1 2 3 4 5 6 7 |
//Sample 04: Create the Circle Object & Display Radius CCircle obj(10); cout<< "Radius: " << obj.GetRadius() << endl; //Sample 05: Set the radius and Display the Radius obj.SetRadius(12); cout<< "Radius: " << obj.GetRadius() << endl; |

Output of Default Constructor Example
What happens when we try to create the object passing no value as shown below?
1 |
CCircle obj2; |
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
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:
1 2 3 4 5 6 7 |
//Sample 04: Create the Circle Object & Display Radius CCircle obj; cout<< "Radius: " << obj.GetRadius() << endl; //Sample 05: Set the radius and Display the Radius obj.SetRadius(12); cout<< "Radius: " << obj.GetRadius() << endl; |
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
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:
1 2 3 4 |
CCircle() { m_radius = 0; } |
Also, we can provide the one parameter constructor as shown below:
1 2 3 4 5 |
//Sample 02: Constructor CCircle(int rad) { m_radius = rad; } |
Below is the completed code and its output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
// TestIt.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> using namespace std; class CCircle { private: //Sample 01: Only one member for Demo purpose int m_radius; public: //Sample 02: Default constructor CCircle() { m_radius = 0; } //Sample 03: Overloaded constructor CCircle(int rad) { m_radius = rad; } //Sample 04: Get and Set methods void SetRadius(int rad) { m_radius = rad; } int GetRadius() { return m_radius; } }; int main() { //Sample 05: Create the Circle Object & Display Radius CCircle obj; cout<< "Radius: " << obj.GetRadius() << endl; //Sample 06: Set the radius and Display the Radius obj.SetRadius(12); cout<< "Radius: " << obj.GetRadius() << endl; //Sample 07: Create the Circle Object & Display Radius CCircle NextObj(6); cout<< "Radius: " << NextObj.GetRadius() << endl; //Sample 08: Set the radius and Display the Radius NextObj.SetRadius(12); cout<< "Radius: " << NextObj.GetRadius() << endl; } |
Output

Default Constructor Example: Final Output
Categories: C++
Tags: Class Initialization, Compiler Provided default constructor, Constructor, Default Constructor