C++ Operator Overloading and Function Overloading




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.


#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, second);
   return 0;
void display(char s[])
   cout << s << endl;
void display(char s[], char t[])
   cout << s << endl << t << endl;




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

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


#include <iostream>
using namespace std;
class Complex
    private :
        double real;
        double imag;
        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

    return 0;
3.1 + i1.5
1.2 + i2.2
4.3 + i3.7

Share this article on