Skip to content
Related Articles

Related Articles

Default Constructors in C++

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 26 May, 2022

A constructor without any arguments or with the default value for every argument is said to be the Default constructor

What is the significance of the default constructor? 

They are used to create objects, which do not have any having specific initial value. 

Is a default constructor automatically provided?

If no constructors are explicitly declared in the class, a default constructor is provided automatically. 

Can a default constructor contain a default argument?

Yes, a constructor can contain default argument with default values.

Will there be any code inserted by the compiler to the user implemented default constructor behind the scenes?

The compiler will implicitly declare the default constructor if not provided by the programmer, will define it when in need. The compiler-defined default constructor is required to do certain initialization of class internals. It will not touch the data members or plain old data types (aggregates like an array, structures, etc…). However, the compiler generates code for the default constructor based on the situation.

Consider a class derived from another class with the default constructor, or a class containing another class object with the default constructor. The compiler needs to insert code to call the default constructors of the base class/embedded object.

C++




// CPP program to demonstrate Default constructors
#include <iostream>
using namespace std;
 
class Base {
public:
    // compiler "declares" constructor
};
 
class A {
public:
    // User defined constructor
    A() { cout << "A Constructor" << endl; }
 
    // uninitialized
    int size;
};
 
class B : public A {
    // compiler defines default constructor of B, and
    // inserts stub to call A constructor
 
    // compiler won't initialize any data of A
};
 
class C : public A {
public:
    C()
    {
        // User defined default constructor of C
        // Compiler inserts stub to call A's constructor
        cout << "C Constructor" << endl;
 
        // compiler won't initialize any data of A
    }
};
 
class D {
public:
    D()
    {
        // User defined default constructor of D
        // a - constructor to be called, compiler inserts
        // stub to call A constructor
        cout << "D Constructor" << endl;
 
        // compiler won't initialize any data of 'a'
    }
 
private:
    A a;
};
 
// Driver Code
int main()
{
    Base base;
 
    B b;
    C c;
    D d;
 
    return 0;
}


Output

A Constructor
A Constructor
C Constructor
A Constructor
D Constructor

There are different scenarios in which the compiler needs to insert code to ensure some necessary initialization as per language requirements. We will have them in upcoming posts. Our objective is to be aware of C++ internals, not to use them incorrectly.

C++




// CPP code to demonstrate constructor can have default
// arguments
#include <iostream>
using namespace std;
class A {
public:
    int sum = 0;
    A(); // default constructor with no argument
    A(int a, int x = 0) // default constructor with one
                        // default argument
    {
        sum = a + x;
    }
    void print() { cout << "Sum =" << sum << endl; }
};
int main()
{
    // This construct has two arguments. Second argument is
    // initialized with a value of 0 Now we can call the
    // constructor in two possible ways.
    A obj1(10, 20);
    A obj2(5);
    obj1.print();
    obj2.print();
    return 0;
}


Output

Sum =30
Sum =5

Explanation : Here, we have a constructor with two parameter- simple parameter and one default parameter. Now, there are two ways of calling this constructor: 

  1. First, we can assign values to both the arguments and these values will be passed to the constructor and the default argument x with value 0 will be overridden by value passed while calling (in this case 20). Hence, code will give an output of 30 (as, sum= a+x i.e 10+20= 30).
  2. Second way is to not pass any value for the default parameter. If you do so, x will take it’s default value 0 as it’s final value and calculate a sum of 5 (as, sum = a+x i.e 5+0=5)
     
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!