What is overloading in C++?​

Creating two or more members that have the same name but are different in number or type of parameter is known as overloading.

In C++, we can overload:

  • Methods
  • Constructors
  • Indexed Properties

We can overload these members because they have parameters only.

Types of overloading

Function overloading

The process of having two or more functions with the same name, but different parameters, is known as function overloading.

The function is redefined by either using different types of arguments or a different number of arguments. It is only through these differences that a compiler can differentiate between functions.

The advantage of function overloading is that it increases the readability of a program because you don’t need to use different names for the same action.

Function overloading example

Let’s see this simple example of function overloading where we change​ the number of arguments of add() method.

// program of function overloading when number of arguments vary.
#include <iostream>
using namespace std;
class Cal {
public:
static int add(int a,int b){
return a + b;
}
static int add(int a, int b, int c)
{
return a + b + c;
}
};
int main(void) {
Cal C; //class object declaration. // class object declaration.
cout<<C.add(10, 20)<<endl;
cout<<C.add(12, 20, 23);
return 0;
}

Explanation

  • We have created two add() methods that have two different numbers of arguments.
  • Now, in the main() function, we call these two methods by passing different numbers of arguments through the same call c.add .
  • The compiler will differentiate based on the number of passing arguments, and execute the correct method accordingly.​

Operator overloading

Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide special meaning to the user-defined data type. Operator overloading is used to overload or redefine most of the operators available in C++. It is used to perform the operation on the user-defined data type.

The advantage of operator overloading is that it can perform different operations on the same​ operand.

Operators that cannot be overloaded are as follows

  • Scope operator (::)
  • Sizeof
  • member selector(.)
  • member pointer selector(*)
  • ternary operator(?:)

C++ operator overloading example

In the example below, we will use operators to overload the + sign.

Let’s look at the example in detail. You can see, in the code below, that the function must specify a return type. Then, it must use the keyword operator followed by the + sign.

//The function that overloads the '+' sign
int operator + (Shape shapeIn)
{
return Area() + shapeIn.Area();
}

To use the overloaded + sign, we just have to use it with our user-defined objects. For example:

int total = sh1 + sh2;

In this case, the + will add the areas of the two shapes.

The final code

#include <iostream>
using namespace std;
class Shape
{
private:
int length; // Length of a box
int width;
public:
// Constructor definition
Shape(int l = 2, int w = 2)
{
length = l;
width = w;
}
double Area()
{
return length * width;
}
int operator + (Shape shapeIn)
{
return Area() + shapeIn.Area();
}
};
int main(void)
{
Shape sh1(4, 4); // Declare shape1
Shape sh2(2, 6); // Declare shape2
int total = sh1 + sh2;
cout << "\nsh1.Area() = " << sh1.Area();
cout << "\nsh2.Area() = " << sh2.Area();
cout << "\nTotal = "<<total;
return 0;
}

Explanation

In this example, We have applied the concept of operator overloading to add the areas of shape1 and shape2.