C++ Operator Overloading and Function Overloading


C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively.

An overloaded declaration is a declaration that had been declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition (implementation).

When you call an overloaded function or operator, the compiler determines the most appropriate definition to use by comparing the argument types you used to call the function or operator with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution.


Function Overloading

If any class have multiple functions with same names but different parameters then they are said to be overloaded. Function overloading allows you to use the same name for different functions, to perform, either same or different functions in the same class.

Function overloading is usually used to enhance the readability of the program. If you have to perform one single operation but with different number or types of arguments, then you can simply overload the function.

Example

#include <iostream>
using namespace std;
/* Number of arguments are different */
void display(char []);  // print the string passed as argument
void display(char [], char []);
int main()
{
   char first[] = "C programming";
   char second[] = "C++ programming";
   display(first);
   display(first, second);
   return 0;
}
void display(char s[])
{
   cout << s << endl;
}
void display(char s[], char t[])
{
   cout << s << endl << t << endl;
}

You can have multiple definitions for the same function name in the same scope.

The definition of the function must differ from each other by the types and/or the number of arguments in the argument list.

You can not overload function declarations that differ only by return type.

Where same function display() is being used to print different data types:

Ouput
C programming
C programming
C++ programming

Operators Overloading

Operator overloading is an important concept in C++. It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it. Overloaded operator is used to perform operation on user-defined data type. For example '+' operator can be overloaded to perform addition on various data types, like for Integer, String(concatenation) etc.

Types of Operator
  • Urinary operator
  • Binary operator

Example

#include <iostream>
using namespace std;
 
class Complex
{
    private :
        double real;
        double imag;
    public:
        Complex () {};
        Complex (double, double);
        Complex operator + (Complex);
        void print();
};
 
Complex::Complex (double r, double i)
{
    real = r;
    imag = i;
}
 
Complex Complex::operator+ (Complex param)
{
    Complex temp;
    temp.real = real + param.real;
    temp.imag = imag + param.imag;
    return (temp);
}
 
void Complex::print()
{
    cout << real << " + i" << imag << endl;
}
 
int main ()
{
    Complex c1 (3.1, 1.5);
    Complex c2 (1.2, 2.2);
    Complex c3;
    
    c3 = c1 + c2; //use overloaded + operator

    c1.print();
    c2.print();
    c3.print();
    return 0;
}
Output
3.1 + i1.5
1.2 + i2.2
4.3 + i3.7

Share this article on