Operator Overloading in C++.

Overloading in C++ (Function and Operator)

  • When we create two or more members of the same name but different in number or parameter form, this is known as C++ overloading.
  • Types of overloading in C++ are:
  1. Function overloading
  2. Operator overloading


www.mskuthar.blogspot.com

C++ Function Overloading
  • Function Overloading is defined as having two or more functions with the same name, but the function overloading in C++ is known differently in parameters. 
  • For overloading function the function is redefined by either using different types of arguments or a different number of arguments. 
  • Compiler can distinguish between functions only through these variations.

// program of function overloading when number of arguments vary.

#include <iostream.h>    
#include <conio.h>  
class Cal {    
    public:    
static int add(int a,int b){      
        return a + b;      
    }      
static int add(int a, int b, int c)      
    {      
        return a + b + c;      
    }      
};     
int main(void) {    
    Cal C;            //     class object declaration.   
    cout<<C.add(10, 20)<<endl;      
    cout<<C.add(12, 20, 23);     
   return 0;    
}    

Expected Output:
30
55


// Program of function overloading with different types of arguments.

#include <iostream.h>    
#include <conio.h>   
int mul(int,int);  
float mul(float,int);   
int mul(int a,int b)  
{  
    return a*b;  
}  
float mul(double x, int y)  
{  
    return x*y;  
}  
int main()  
{  
    int r1 = mul(6,7);  
    float r2 = mul(0.2,3);   
    std::cout << "r1 is : " <<r1<< std::endl;  
    std::cout <<"r2 is : "  <<r2<< std::endl;  
    return 0;  
}  
Expected Output:-

r1 is : 42

r2 is : 0.6  

C++ Operator Overloading

  • Operators ' definition is already specified and fixed in C++ language for basic types such as: int, float, double etc. For example: Then + operator will be used if you want to add two integers.
  • Nevertheless, you can specify the definition of the operator for user-defined forms (like: objects), i.e. you can redefine the way the operator operates. 
  • For example: If there are two class objects containing string as their data member, you can use+ Operator to concatenate two strings. 
  • Suppose you can use+ operator to add integers instead of strings if that class includes integral data member. 
  • This function is known as operator overloading in C++ programming which allows programmers to redefine the operator's meaning while operating on class objects.
Why Operator overloading is used in C++ programming?
  • You can write any C++ program without Operator overloading information. 
  • Nonetheless, running the operator is widely used by programmers to make a program clearer. 
  • For example: you can replace the code as: calculation= add(mult(a, b),div(a, b)); with calculation= a*b+a / b; which is easier to read and understand.
How to overload operators in C++ programming?
  • A role of an operator is specified within a class to overload an operator as:
www.mskuthar.blogspot.com
  • The return type comes first which is followed by keyword operator, followed by operator sign,i.e., the operator you want to overload like: +, <, ++ etc. and finally the arguments is passed. 
  • Then, inside the body of you want perform the task you want when this operator function is called.
  • This operator function is called when, the operator(sign) operates on the object of that class class_name.


Example of operator overloading in C++ Programming

/* Simple example to demonstrate the working of operator overloading*/
#include <iostream.h>
#include <conio.h>
class temp
{
   private:
      int count;
   public:
       temp():count(5){  }
       void operator ++() { 
        count=count+1; 
       }
       void Display() { cout<<"Count: "<<count; }
};
int main()
{
    temp t;
    ++t;        /* operator function void operator ++() is called */
    t.Display();
    return 0;
}
Expected Output:-
Count: 6
Explanation
  • In this program, a operator function void operator ++ () is defined(inside class temp), which is invoked when ++ operator operates on the object of type temp. 
  • This function will increase the value of count by 1.

Things to remember while using Operator overloading in C++ language

  • Operator overloading can not be used to adjust the manner in which operator works on built-in forms. Operator overloading allows only the user-defined forms to redefine the operator's purpose.
  • Two operators are allocated operator(=) and address operator (&) that need not be overloaded. 
  • Since those two operators in the C++ library are already overloaded. 
  • When obj1 and obj2 are two objects of the same class, for example, you can use obj1=obj2; without overloading= operator. 
  • This code copies object contents into obj1. Similarly, you can directly use the address operator without overloading which will return the object address to memory.
  • Operator overloading can not alter operator precedence and operator associativity. 
  • Yet if you want to modify the evaluation order you can use parenthesis. Not all C++ language operators can be overloaded.
  • The operators that cannot be overloaded in C++ are ::(scope resolution), .(member selection), .*(member selection through pointer to function) and ?:(ternary operator).

Example for using operator overloading
  • Operator overloading allows programmer to define operator the way they want but, there is a pitfall if operator overloading is not used properly. 
  • In above example, you have seen ++ operator operates on object to increase the value of count by 1. 
  • But, the value is increased by 1 because, we have used the code:


void operator ++() { 
        count=count+1; 
       }
  • If the code below was used instead, then the value of count will be decreased by 100 if ++ operates on object.

void operator ++() { 
        count=count-100; 
       }
  • But, it does not make any sense to decrease count by 100 when ++ operator is used. 
  • Instead of making code readable this makes code obscure and confusing. 
  • And, it is the job of the programmer to use operator overloading properly and in consistent manner.
  • Again, the above example is increase count by 1 is not complete. This program is incomplete in sense that, you cannot use code like:

     t1=++t
  • It is because the return type of operator function is void. It is generally better to make operator work in similar way it works with basic types if possible.

Post a Comment

Previous Post Next Post