Home >>C++ Tutorial >C++ method Overloading

C++ method Overloading

C++ method Overloading (Function and Operator)

If the members that are two or more than two in numbers possesses the very same name but are different in terms of type of parameters or different in number is generally called as overloading in C++.

These following members that are displayed below can be overloaded in C++:

  • Methods
  • Constructors
  • Indexed properties

The reason that these members can only be overloaded is that they only have the parameters in them.

Types of overloading in C++

Here are the types of the overloading in C++:

  • Function/Method overloading
  • Operator overloading

1. Function Overloading

The procedure that has two or more than two functions with the same name and is different in parameters is generally known as the function overloading in C++. The functions gets redefined in the function overloading generally by two ways, either by using different types of arguments or a different number of arguments. These are the differences that generally help the compiler in differentiating between various functions.

There is a main advantage of the function overloading and that is, enhanced readability of the program that gives freedom to the programmers from using different names for the same action.

C++ Function Overloading Example

Here are a couple of the example of the function overloading in C++ that will help you understand the subject more clearly:

#include <iostream>    
using namespace std;    
class Calculator 
{    
    public:    
	static int sum(int x,int y)
	{      
        return x + y;      
    }      
static int sum(int x, int y, int z)      
    {      
        return x + y + z;      
    }      
};     
int main(void) 
{    
    Calculator obj;    
    cout<<"Sum of two number="<<obj.sum(10, 11)<<endl;      
    cout<<"Sum of three number="<<obj.sum(10, 11, 12);     
   return 0;    
}    
Output :
Sum of two number=21
Sum of three number=33

C++ Operators Overloading

A compile-time polymorphism that consists of the overloaded operators, in order to deliver the special meaning to the user-defined data type. This method is generally used to overload or redefine the most of the operators that are available in the C++ programming language. In order to perform the operation on the user-defined data type this method is used. Just for instance, C++ provides the ability to add the variables of the user-defined data type that are basically applied to the data types that are built-in.

Different operations can be performed on the same operand by the operators overloading and it is known as the main advantage of it.

Here are the operators that cannot be overloaded are mentioned below:

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

Syntax of Operator Overloading

Here is the syntax for the operator overloading:

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

Here is the break-down of the syntax:

  • return type : This is the type of the value that is returned by the function.
  • class_name : It is generally the name of the class.
  • operator op : This is basically an operator function and op is the overloaded operator and operator is also the keyword.

Rules for Operator Overloading

  • Operators that are existing can be overloaded but the new operators cannot be overloaded.
  • The overloaded operator generally consists of atleast one operand that is of the user-defined data type.
  • Friend function can be used by the programmers to overload certain operators in C++. But, in order to overload those operators the member function can be used.
  • Whenever the unary operators are overloaded via a member function take none of the explicit arguments, but, when they are being overloaded just by a friend function it takes one argument.
  • Whenever the binary operators are being overloaded via a member function takes one explicit argument but if these operators are overloaded via a friend function then it will take two explicit arguments.

C++ Operators Overloading Example

Here are the example of the operator overloading in the C++ that will explain the concept better and help you understand the core of the method:

#include     
using namespace std;    
class Demo    
{    
   private:    
      int number;    
   public:    
       Demo(): number(10)
	   {
	   
	   }    
       void operator ++()         
	   {     
          number = number+5;     
       }    
       void Print() 
	   {     
           cout<<"The Numebr value is = "<
Output :The Numebr value is = 15