C++ Overloading

C++ overloading can be defined as a technique to use a single identifier to define various methods or techniques of a class that differs in their input and output parameters. The concept of overloading is generally used when a program block conceptually executes the same task but with a slight distinctness in a set of parameters. Overloading is used to avoid redundant code where the same method name or operator is used multiple times but with a different set of parameters or number of operands.

C++ Overloading

The actual method that gets called during run time is resolved at compile-time, thus avoiding run-time errors. Overloading provides code clarity, reduce complexity, and increases runtime presentation of a code. There are two different types of overloading present in C++. They are as follows:

  • Function Overloading
  • Operator Overloading

Function Overloading in C++

A function overloading in C++ can be defined in such a way if a class has multiple member functions with the same name but different parameters and programmers can be used to perform similar operations known as “function overloading“. The function overloading can be considered as an example of the “Polymorphism”  feature in C++. When an overloaded function is called from the main program the function with matching arguments is invoked.



Syntax:

int test ( ) { }
int test ( int a) { }
int test ( int b, double c) { }
double test ( double a) { }

Example:

#include <iostream> 
using namespace std; 
  
void print(int i) { 
  cout <<"Here is int "<< i <<endl; 
} 
void print(double  f) { 
  cout<<"Here is float"<< f <<endl; 
} 
void print(char const *c) { 
  cout<<"Here is char* "<< c <<endl; 
} 
  
int main() { 
  print(10); 
  print(10.10); 
  print("ten"); 
  return 0; 
}

Output:

Here is int 10
Here is float 10.1
Here is char* ten

Function with default arguments

A function with default arguments can be defined by default value for a parameter while declaring the function, it is said to be as default argument.

Example:

sum(int x, int y=0)
{
    cout << x+y;
}
int main()
{
    sum(10);
    sum(10,0);
    sum(10,10);
}

Operator Overloading in C++

The operator overloading in C++ can be used at a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. It is used to overload the operator in C++ and performs the operation on the user-defined data type.

We can overload almost any operator in C++ but there are some of the operators which cannot be overloaded. They are as follows:

  • Member selector – (.)
  • Ternary operator – (?:)
  • Scope operator – (::)
  • Member pointer selector – (*)
  • Size

Rules for Overloading Operators

  • Only existing operators can be overloaded.
  • Programmers cannot change the basic meaning of an operator.
  • Overloaded operators will always follow the syntax rules of the original operators.
  • Programmers cannot use friend function to overload certain operators.

Example:

#include<iostream> 
using namespace std; 
  
class Complex { 
private: 
    int real, imag; 
public: 
    Complex(int r = 0, int i =0)  {real = r;   imag = i;} 
      
    // This is automatically called when '+' is used with 
    // between two Complex objects 
    Complex operator + (Complex const &obj) { 
         Complex res; 
         res.real = real + obj.real; 
         res.imag = imag + obj.imag; 
         return res; 
    } 
    void print() { cout << real << " + i" << imag << endl; } 
}; 
  
int main() 
{ 
    Complex c1(10, 5), c2(2, 4); 
    Complex c3 = c1 + c2; // An example call to "operator+" 
    c3.print(); 
}

Output:

12 + i9