Open In App

Default Constructors in C++

Last Updated : 20 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A default constructor is a constructor that 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.

If a default constructor is not explicitly defined by the programmer in the source code, the compiler automatically generates one during the compilation process. However, if a programmer explicitly defines a default constructor, the compiler does not generate it. Instead, the explicitly defined default constructor is called implicitly wherever needed.

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.

Default Constructors and Inheritance:

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


Constructors and Default Arguments:

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: 

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).

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).

FAQs

1. What is the significance of the default constructor? 

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

2. Is a default constructor automatically provided?

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

3. Can a default constructor contain a default argument?

Yes, a default constructor either has no parameters, or if it has parameters, all the parameters have default arguments.

4. 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.



Next Article
Practice Tags :

Similar Reads