Tutorial Study Image

Constructor overloading in C++


May 25, 2023, Learn eTutorial
401

Constructor overloading in C++ will be covered in this tutorial with the help of some examples. It is very important to understand the concept of constructors first in order to fully grasp constructor overloading. Constructors are one-of-a-kind methods that are automatically called whenever we construct a class object. Initializing the data members of a new object is the main goal of the constructor. In a similar manner to function overloading, constructors can also be overloaded.

The class name is the same for overloaded constructors, but they take a different amount of arguments. The appropriate constructors are called depending on how many and what types of arguments are passed. You will learn everything about

What do you mean by constructor overloading in C++?

As long as each constructor in C++ has a unique list of arguments, we are allowed to have many constructors in a class with the same name. This idea is referred to as constructor overloading.

Every constructor shares the same name as the class, but they vary depending on the number of arguments, the datatypes of the arguments, or a combination of the two. When using constructor overloading in C++, we can have multiple constructors in the same class as long as they each take a different set of parameters and It is very much similar to function overloading.

Constructor overloading is the concept of having numerous constructors with distinct parameters in a single class. The essential point to remember here is that constructors will execute according to the arguments. For example, if a program contains three constructors with 0 arguments, one argument, and two arguments, So, if we supply one argument to the constructor, the compiler will run the constructor that takes one argument.

The program that exemplifies the C++ constructor overloading


#include <iostream>
using namespace std;

class Complex
{
    int a, b;

public:
    Complex(){
        a = 0;
        b =0;
    }

    Complex(int x, int y)
    {
        a = x;
        b = y;
    }

    Complex(int x){
        a = x;
        b = 0;
    }

  

    void printNumber()
    {
        cout << " So your number is " << a << " + " << b << "i" << endl;
    }
};
int main()
{
    Complex c1(8, 10);
    c1.printNumber();

    Complex c2(9);
    c2.printNumber();

    Complex c3;
    c3.printNumber();
    return 0;
}

 

Output:


So your number is 8 + 10i
So your number is 9 + 0i
So your number is 0 + 0i

Explanation

  • First, a "complex" class with the private data elements "a" and "b" was constructed.
  • The default constructor of the "complex" class is then declared, which has no parameters and it assigns "0" to the data elements "a" and "b."
  • It is established that the "complex" class has a parameterized constructor that accepts two parameters as well as assigns values to the data members "a" and "b."
  • A parameterized constructor of the "complex" class is declared, which will take one parameter and set the values to the data elements "a" and "b."
  • The data elements "a" and "b" have values, and the function "printNumber" is defined to print those values.
  • It is possible to call a parameterized function constructor by passing it the object "c1" and the numbers "8" and "10." The most important thing to keep in mind is that the function constructor will be executed with two parameters.
  • The function "printNumber" is called, and the values of data members are printed.
  • The parameterized constructor is invoked with the object "c2" as well as the value "9" is passed. The most important thing to keep in mind is that this will call the constructor with a single parameter.
  • It is necessary to print the values of the data members using the function "printNumber."
  • The object "c3" is used to call the default constructor.
  • The function "printNumber," which prints the values of the data members, is called.

Different types of C++ overloading

Mainly there are two types of overloading

  • Function overloading
  • Operator overloading

Function overloading

Function overloading is used to make programs easier to read. It is not required to use separate names for the same action when using function overloading. Function overloading in CPP is accomplished by calling multiple functions with the same name but different parameters.

Operator overloading

Operator overloading is used to perform many operations on the same operand. Operator overloading involves performing operations on user-defined data types to increase the number of CPP operators.

Benefits of constructor overloading in C++

The advantage of a constructor overloading in C++ is that it allows for the creation of several types of objects belonging to a class by having additional constructors in a class. In fact, it is comparable to compile-time polymorphism, which is another name for C++ function overloading. Because of this, C++ needs constructor overloading if we want to construct an object in a different way.

Difference between constructor overloading and method overloading

Constructor overloading

  • Constructor Overloading is the practice of writing many constructors in a class, each with a unique set of arguments.
  • Every constructor will include the class name.
  • When creating an object, an overloaded constructor will be used.
  • A constructor that is overloaded cannot be static since a constructor is connected to the formation of an object.
  • Considering that a constructor is not inherited from subclasses, an overloaded constructor cannot be final, as this is inconsistent.
  • To stop outsiders from using an overloaded constructor to create objects outside of the class, it can be made private.

Method overloading

  • Method overloading is the practice of creating many methods with distinct sets of arguments within a single class.
  • Every method needs to have the same name.
  • If a method is overloaded but not static, it can only be called after the required object has been created.
  • Static methods that are overloaded can be accessed without first constructing an object
  • An overloaded method can be final to avoid overriding by subclasses.
  • To block access from calling an overloaded method from outside the class, a method can be made private.

Key points to remember

  • The only real difference between overload constructors and standard constructors is the number and type of arguments they accept.
  • Depending on how many and what kind of arguments are supplied, a function constructor is called.
  • Arguments must be supplied to the compiler when generating the object in order for it to know which constructor to call.