Open In App

Default Constructors in C++

Last Updated : 30 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A default constructor is a constructor that can be called without any arguments while creating an object. It can either takes no arguments or has default values for all its parameters. It is also referred to as a zero-argument constructor when it explicitly accepts no arguments.

The C++ compiler automatically generates a default compiler for every class during the compilation process. But if there is any user defined constructor present in the class, the compiler does not generate it.

Example 1: No User Defined Constructor

C++
class GfG {
    public:
    // No Constructor
};

In the above class, a default constructor will be generated by the compiler at compile time.

Example 2: Explicitly Defined Default Constructor

C++
class GfG {
    public:
    
    // Default Constructor i.e. constructor
    // with no arguments
    GfG() {}
};

In the above class, as programmer defined a constructor with no argument (default constructor), so no compiler generated default constructor will be needed.

Example 3: Explicitly Defined Parameter Constructor

C++
class GfG {
    public:
    
    // Parameterized constructor
    GfG(int a) {}
};

In the above class, a parameterized constructor is defined but not default constructor, but even in this case, the compiler does not generate the default constructor. The same happens if you define other type of constructor explicitly.

Parameterized Constructor with Default Values

We can define default values for the parameters in the parameterized constructor. This allows us to call the parameterized constructor without arguments.

Example:

C++
#include <iostream>
using namespace std;

class GfG {
public:
    int a;
    
    // Parameterized constructor with
    // default value
    GfG(int val = 22) {a = val;}
};

int main() {
    
    // Creating object with argument
    GfG gfg1(109);
    
    // Creating object without argument
    GfG gfg2;
    
    cout << gfg1.a << endl;
    cout << gfg2.a;
    return 0;
}

Output
109
22

Now, the questions arise, does a parameterized constructor with default values can be called a default constructor?

The answer is yes, because any constructor that can be called without parameters is called default constructor according to C++ standard. Though it will not be a zero-parameter constructor.

Default Constructors and Inheritance

In cases where a class is derived from a base class with a default constructor, or a class contains an object of another class with a default constructor, the compiler must insert code to ensure these default constructors are invoked appropriately for the base class or the embedded objects.

C++
#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 {
    A a;
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'
    }
};

// Driver Code
int main()
{
    Base base; // Only Base constructor (default provided by the compiler) is called
    B b; // Calls A's constructor due to inheritance (compiler-generated constructor for B)
    C c; // Calls A's constructor first, then C's constructor
    D d; // Calls A's constructor for member 'a', then D's constructor

    return 0;
}

Output
A Constructor
A Constructor
C Constructor
A Constructor
D Constructor

Next Article
Practice Tags :

Similar Reads