Detailed Explanation of Overloading in C++

If we create two or more members with the same name but different numbers or types of parameters, this is called C++ overloading. In C++, we can overload:

  • Methods

  • Constructors

  • Index properties

This is because these members only have parameters.

Types of Overloading in C++:

  • Function Overloading

  • Operator Overloading

Detailed Explanation of Overloading in C++

Function Overloading in C++

Function overloading is defined as the process of having two or more functions with the same name but different parameters, known as function overloading in C++. In function overloading, the function is redefined using different types or numbers of parameters. Only through these differences can the compiler distinguish between these functions.

The advantage of function overloading is that it increases the readability of the program, as you do not need to use different names for the same operation.

đŸ‘‡ Click to Claim đŸ‘‡
đŸ‘‰ C Language Knowledge Material Collection

Example of Function Overloading in C++

Let’s look at a simple example of function overloading, where we change the number of parameters in the add() method.

// Example code for function overloading when the number of parameters is different.
#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.
  cout << C.add(10, 20) << endl;
  cout << C.add(12, 20, 23);
  return 0;
}

Output:

30
55

Let’s look at a simple example where the parameter types are different.

// Example program for function overloading with different parameter types.
#include<iostream>
using namespace std;
int mul(int, int);
float mul(float, int);
int mul(int a, int b){
  return a * b;
}
float mul(double x, int y){
  return x * y;
}
int main(){
  int r1 = mul(6, 7);
  float r2 = mul(0.2, 3);
  std::cout << "r1 is : " << r1 << std::endl;
  std::cout << "r2 is : " << r2 << std::endl;
  return 0;
}

Output:

r1 is : 42
r2 is : 0.6

Function Overloading and Ambiguity

When the compiler cannot determine which function to call among the overloaded functions, this situation is called function overloading ambiguity.

When the compiler shows an ambiguity error, it will not run the program.

Reasons for function overloading ambiguity:

  • Type conversion.

  • Functions with default parameters.

  • Functions with reference passing.

  • Detailed Explanation of Overloading in C++

  • Type conversion:

Let’s look at a simple example.

#include<iostream>
using namespace std;
void fun(int);
void fun(float);
void fun(int i){
  std::cout << "The value of i is: " << i << std::endl;
}
void fun(float j){
  std::cout << "The value of j is: " << j << std::endl;
}
int main(){
  fun(12);
  fun(1.2);
  return 0;
}

The above example shows the error “call of overloaded ‘fun(double)’ is ambiguous”. fun(10) will call the first function. According to our prediction, fun(1.2) calls the second function. However, this does not reference any function because in C++, all floating-point constants are treated as double rather than float. If we replace float with double, the program will work correctly. Thus, this is a type conversion from float to double.

  • Functions with default parameters

Let’s look at a simple example.

#include<iostream>
using namespace std;
void fun(int);
void fun(int, int);
void fun(int i){
  std::cout << "The value of i is: " << i << std::endl;
}
void fun(int a, int b = 9){
  std::cout << "The value of a is: " << a << std::endl;
  std::cout << "The value of b is: " << b << std::endl;
}
int main(){
  fun(12);
  return 0;
}

The above example shows the error “call of overloaded ‘fun(int)’ is ambiguous”. fun(int a, int b = 9) can be called in two ways: one is by calling the function with one parameter, which is fun(12), and the other is by calling the function with two parameters, which is fun(4,5). The fun(int i) function is called with one parameter. Thus, the compiler cannot choose between fun(int i) and fun(int a, int b = 9).

  • Functions with reference passing

Let’s look at a simple example.

#include <iostream>
using namespace std;
void fun(int);
void fun(int &);
int main(){
  int a = 10;
  fun(a); // Error, which f()?
  return 0;
}
void fun(int x){
  std::cout << "The value of x is: " << x << std::endl;
}
void fun(int &b){
  std::cout << "The value of b is: " << b << std::endl;
}

The above example shows the error “call of overloaded ‘fun(int&)’ is ambiguous”. The first function accepts an integer parameter, while the second function accepts a reference parameter. In this case, the compiler does not know which function the user wants because there is no syntactical distinction between fun(int) and fun(int &).

Operator Overloading in C++

Operator overloading is a form of compile-time polymorphism where operators are overloaded to provide special meaning for user-defined data types. Operator overloading is used to overload or redefine most of the operators provided in C++. It is used to perform operations on user-defined data types. For example, C++ provides the ability to add built-in data types.

The advantage of operator overloading is that different operations can be performed on the same operands.

Operators that cannot be overloaded are as follows:

Scope operator (::)

  • Sizeof

  • Member selection operator (.)

  • Member pointer selection operator (*)

  • Conditional operator (?:)

Syntax for Operator Overloading

return_type class_name::operator op(argument_list) {
// Function body
}

Where return type is the type of value returned by the function.

class_name is the name of the class.

operator op is the operator function, where op is the operator to be overloaded, and operator is the keyword.

Rules for Operator Overloading

  • Only existing operators can be overloaded; new operators cannot be overloaded.

  • The overloaded operator must have at least one operand of a user-defined data type.

  • Certain operators cannot be overloaded using friend functions, but can be overloaded using member functions.

  • When a unary operator is overloaded through a member function, no explicit parameter is needed, but if overloaded through a friend function, one parameter is needed.

  • When a binary operator is overloaded through a member function, one explicit parameter is needed, and if overloaded through a friend function, two explicit parameters are needed.

Example of Operator Overloading in C++

Let’s look at a simple example of operator overloading in C++. In this example, the void operator++() operator function is defined (inside the Test class).

// Program to overload the unary operator ++.
#include <iostream>
using namespace std;
class Test {
  private:
  int num;
  public:
  Test(): num(8) {}
  void operator++() {
    num = num + 2;
  }
  void Print() {
    cout << "The value of Count is: " << num;
  }
};
int main() {
  Test tt;
  ++tt; // Call function "void operator++()"
  tt.Print();
  return 0;
}

Output:

The value of Count is: 10

Let’s look at a simple example of overloading a binary operator.

// Program to overload a binary operator.
#include <iostream>
using namespace std;
class A {
  int x;
    public:
    A() {}
    A(int i) {
       x = i;
    }
    void operator+(A);
    void display();
};
void A::operator+(A a) {
  int m = x + a.x;
    cout << "The result of adding two objects is: " << m;
}
int main() {
  A a1(5);
  A a2(4);
  a1 + a2;
  return 0;
}

Output:

The result of adding two objects is: 9

Detailed Explanation of Overloading in C++


Popular Recommendations
  • CLion Tutorial – CMake Macros in CLion

  • C Language Algorithm – “Split Linked List” Algorithm Problem

  • C++ Tutorial – Detailed Explanation of Function Overriding in C++

Leave a Comment