C++ Overloading (Function and Operator)

If we create two or more members having the same name but different in number or type of parameter, it is known as C++ overloading. In C++, we can overload:

  • methods,
  • constructors, and
  • indexed properties

It is because these members have parameters only.

Types of overloading in C++ are:

  • Function overloading
  • Operator overloading
C++ Overloading

C++ Function Overloading

Function Overloading is defined as the process of having two or more functions with the same name, but different parameters are known as function overloading in C++. In function overloading, the function is redefined by using either different types of arguments or a different number of arguments. It is only through these differences compiler can differentiate between the functions.


C++ Function Overloading Example

Let's see the simple example of function overloading where we are changing number of arguments of add() method.


    1. #include <iostream>    
    2. using namespace std;    
    3. class Cal {    
    4.     public:    
    5. static int add(int a,int b){      
    6.         return a + b;      
    7.     }      
    8. static int add(int a, int b, int c)      
    9.     {      
    10.         return a + b + c;      
    11.     }      
    12. };     
    13. int main(void) {    
    14.     Cal C;                  //     class object declaration.   
    15.     cout<<C.add(10, 20)<<endl;      
    16.     cout<<C.add(12, 20, 23);     
    17.    return 0;    
    18. }    

Output:-

30
55

C++ Operators Overloading

👉Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform the operation on the user-defined data type. For example, C++ provides the ability to add the variables of the user-defined data type that is applied to the built-in data types.

👉The advantage of Operators overloading is to perform different operations on the same operand.

Operator that cannot be overloaded are as follows:

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

Syntax of Operator Overloading:-

  1. return_type class_name  : : operator op(argument_list)  
  2. {  
  3.      // body of the function.  
  4. }  

C++ Operators Overloading Example

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


  1. #include <iostream>    
  2. using namespace std;    
  3. class Test    
  4. {    
  5.    private:    
  6.       int num;    
  7.    public:    
  8.        Test(): num(8){}    
  9.        void operator ++()         {     
  10.           num = num+2;     
  11.        }    
  12.        void Print() {     
  13.            cout<<"The Count is: "<<num;     
  14.        }    
  15. };    
  16. int main()    
  17. {    
  18.     Test tt;    
  19.     ++tt;  // calling of a function "void operator ++()"    
  20.     tt.Print();    
  21.     return 0;    
  22. }

Output:-
The Count is: 10