C++

What is Constructor Overloading in C++

In C++ “object-oriented programming” (OOP), creating a class in which we declare member functions, such as “constructors” is an important feature. Constructor is known as a special member function of the class which is called by their objects.

This guide will analyze the constructor overloading concept in C++.

What is Constructor Overloading in C++?

In C++, special member functions are called constructors which are used to initialize class objects. Whenever an object is created, it is automatically called. In addition, “constructor overloading” refers to the definition of many constructors in a class with an identical name but different parameters.

Syntax of Constructor Overloading

In C++, constructor overloading follows the same syntax as conventional function overloading. For instance, let’s check out the provided code:

class my_Class {
public:
    my_Class(int param1) {
    }
    my_Class(int param1, int param2) {
        // code initialization
    }
    // ...more constructors
};

Here, we have declared class “my_Class” with two constructors. The initial constructor has a single integer type parameter. However, the second constructor takes two integer parameters.

Example: Constructor Overloading in C++

Let’s take a look at the provided example that demonstrates the concept of constructor overloading in C++:

#include <iostream>

using namespace std;

In the above snippet:

  • <iostream>” is included as a header file, which is used for different operations, such as input and output.
  • using namespace std;” statement facilitates users to access the STL(Standard Template Library) objects and functions without explicitly specifying the prefix “std”.

In the next code block, a “MyConstruct” class is defined that has two constructors one with no parameter and the other with two parameters. Without parameter constructor is the default constructor that has a member variable “total_area” initialized with “0”. The other constructor accepts “num1”, and “num2” integer values as parameters. Lastly, the member variable is used to save the calculated product of these integer values:

class MyConstruct
{
public:
    int total_area;
    MyConstruct()
    {
        total_area = 0;
    }
    MyConstruct(int num1, int num2)
    {
        total_area = num1 * num2;
    }
};

After that, inside the “main()” function, we created two objects “obj1” and “obj2” with the same name of constructors as the class “MyConstructor”. The “obj1” takes no parameter values so its “total_area” will be “0”. The “obj2” takes two values with “length=100” and “width=40”. Lastly, used the “cout” with the insertion operator to display the calculated area of the object:

int main()

{

  MyConstruct obj1;

  MyConstruct obj2( 100, 40);

  cout << "Area of obj1: " << obj1.total_area<< endl;

  cout << "Area of onj2: " << obj2.total_area<< endl;

  return 1;

}

The area of the obj1 and obj2 has been displayed in the provided output:

Note: By using Constructor overloading, we increase the reusability of code and also simplify the creation of objects in classes.

Conclusion

In C++, Constructor overloading is a useful feature that enables the definition of several constructors within the same class, each with a different set of parameters. It encourages code reuse and offers flexibility in object initialization. This guide provided a C++ example of constructor overloading.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.