C++

Default Constructor in C++

“When an object is created in C++, a special technique named the function Constructor is immediately called. In general, it is utilized to configure member functions of new classes. In C++, the class name serves as the title of the Constructor. Whenever we create an object, we have to invoke the Constructor. It creates the values, i.e., supplies the element with data; this is why it is referred to as a Constructor.

The default constructor is utilized to generate the objects without a predefined initial value. A constructor can have default parameters as well as default values. If the user does not specify a default constructor, the compiler may automatically construct one and define it as required. Some configurations of the class intervals must be performed by the default constructor specified by the compiler. But depending on the situation, the compiler creates instructions for the default constructor.

We’ll go over the default constructor’s functionality in this article.”

Example no 1

Let’s define a class that is inherited by some other class having a default constructor or create a class that contains an element of some other class having a default constructor. The integrated object of the class and the integrated object of the default constructors must be invoked by the compiler by incorporating the program.

#include <iostream>
using namespace std;

class Base {
public:
};

class X {
public:
    X() { cout << "X Constructor" << endl; }
    int size;
};
class L : public X {
};
class M : public X {
public:
    M()
    {
        cout << "M Constructor" << endl;
    }
};
class N {
public:
    N()
    {
        cout << "N Constructor" << endl;
    }

private:
    X x;
};
int main()
{
    Base base;

    L l;
    M m;
    N n;
    return 0;
}

First of all, we will integrate the library <iostream>. This header file will contain different input and output functionalities. Then we will utilize the standard namespace. In the next step, we will create a Base class. Here the compiler will declare the constructor. And we set it to public. Now we will create a class named X. The user-defined constructor will be called publicly. We will initialize a variable “size”.

Further, we will create an object of class “L”. The object of this class will be set as public. Here the compiler defines the default constructor of the “L” class, and we insert the class to call the object constructor. The compiler would not initialize any value of class X. Now again, we create a new class named M. We construct the object of the class M, and then we specify it as public.

Within this class, we create a user-defined constructor of class M. The compiler will integrate the class to invoke the X constructor. The “cout” statement will be utilized to show the outcome. Let’s create another class, N. We publically create a constructor of this class. This will be the user-defined constructor. Then the “cout” command is inserted to show the result.

Here the compiler once again didn’t initialize any content related to class N. Further, we would construct the object “x” of the class “X”. And this element of the class could be kept private. Now we call the main() function. We invoke the objects of different classes.

Example no 2

The compiler may have to insert code in a variety of situations to verify that a certain configuration is required by the language. The default constructor in this instance has two arguments: a simple argument and a default argument. There are currently two methods for invoking the default constructor:

Firstly, we can give both values of the parameters that will be provided to the default constructor, substituting the default parameter with the value that was provided when executing the default constructor.

The second approach is being used to indicate the default argument is empty. When you do this, “j” will compute a summation of 5 and utilize its default value of 0 as its desired value.

#include <iostream>
using namespace std;
class F {
public:
    int s = 0;
    F();
    F(int i, int j = 0)
                   
    {
        s = i + j;
    }
    void print() { cout << "Sum of two numbers =" << s << endl; }
};
int main()
{
    F o_1(23, 50);
    F o_2(5);
    o_1.print();
    o_2.print();
    return 0;
}

Here we are going to include the library <iostream>. Then we utilize the standard namespace as std. Now we create the class named “F”. Within this class, we will initialize the variable termed “s”. Here the default constructor holds no arguments. The name of the constructor and the class will be the same. We create another default constructor of the class “F”. But here, we pass two variables (i and j) as the parameter of the constructor.

Moreover, we will employ the formulae of the sum. Here we add the value of one variable to another. Then we call the void print() method, and along with this, we utilize the “cout” statement to print the statement “Sum of two values”. Now let’s start the coding inside the body of the main() function after calling this function. We create two different objects of class F. The first object, “o_1,” has two integer values as its arguments. The second variable, “o_2,” contains only 1 value as the parameter.

We would invoke the constructor in these two possible approaches. We utilize the print() method for both these objects respectively to show the output. In the end, we would have been using the “return 0” statement.

Conclusion

In this post, we spoke about the operation of the default constructor. The constructor retains a similar title as the class, is open to the public, and therefore contains no return type. In a default constructor, we will provide no parameters. We have run a variety of programs, one of which shows how to use the default constructor. We illustrate that constructors would have default parameters in the second example. We create two default constructors, one with no arguments and the other with two arguments, and then we evaluate their functionality.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.