Topic : Operator Overloading in C++ Programming Language:

Operator Overloading:



We have already seen Overloading many times. Let's take an example of overloading. In our all examples, we are using Stream Insertion Operator (<<)with cout to print somethings on screen. sometime we print int, sometimes char, sometimes double or float, and sometimes string, as shown below:



cout<< "string";
cout<< int;
cout<< char;
cout<< float;
cout<< double;


We can see that, we have only one Operator << but we can use it for many data types, its mean this operator is overloaded for many data types. Header file iostream contains prototypes for all those function through which we overload this operator.



Why we need to Overload Operator?

If we can't overload the operator in C++, then there must be separate Insertion Operator for all data types. but due to Operator overloading, we have single operator work for many data types. Let's take another example, we have + Operator. we use this operator to add int, float or double. but how about, if we need to add two Object? could we add two object using this + operator. No, we can't, because We know that, computers are dumb Machines and they can't decide anything on their own. they don't know how to add two objects, and if we try to add two objects using + Operator, we will get an error. so We Need to Overload + Operator to add two objects.



Operator overloading allows us to redefine the way operator works for user-defined types Like objects or structures. some libraries already contain the Prototype and function of the + operator. so C++ Compiler knows how to add 2 integers using + operator. but it doesn't know how to add two objects. so we need to overload the operator, mean we need to add another function using the same operator, mean overload the operator.



How to Overload the Operator?

Operator Overloading can be done by Implementing a Function which can be a Member function of class, Non-member function of class, or a Friend function of class. but in this tutorial we will Implement the function as Member function of class. To overload an Operator, we need to define an special Operator function inside the class as:



class className
{
   public:
      return operator Operator_symbol (arguments_List)
      {
         //function body:
      }   
      ...
      ...
};
 
 


In above syntax, We need to use the keyword operator after the return type. after operator we need to mentioned the Operator symbol (+, -, *, or /, etc.) that we want to overload. and we can pass any arguments to the operator function in similar way as functions: and we define this function as public member of class. let's have an example here to know how to overload the operator in C++:



/*Example: Operator overloading: InfoBrother:*/
 
#include <iostream>
using namespace std;
 
class Overload
{
   private:
      int counter;
 
   public:
      Overload() //constructor
      {
         counter=5; //assign value to counter:
      } 
 
      void operator ++() //++ Operator overloading:
      {  
          counter = counter+1; 
      }
 
      void Display() //function to display value:
      { 
          cout<<" Counter is: "<<counter<<endl;
      }
};
 
int main()
{
   Overload load; //Object creation:
   cout<<" Before Applying Overload Operator: "<<endl;
   load.Display();
 
   ++load; //calling Operator Overloading function:
   cout<<" \n After Applying Overload Operator: "<<endl;     
   load.Display();
 
   return 0;
}


In our above Example, we overload Increment Operator ++ to increment the value of our object. when we try to increment the object, then the object call overloaded operator instead of default operator. because normal or default Increment Operator can't increment the object.



Tip:
Remove the Overload Operator function from class, and try to compile the program, and check if you can increment the Object value using default increment Operator.



We can Overload These Operators:

+-*/%^
&|~!,=
--++<=>=<>
>><<==!=&&||
+=-=/=%=^=&=
()[]<<=>>=*=|=
->->*newnew[]deletedelete[]

We can't Overload these Operator:

::.*.?:


  • More about Operator Overloading.

    » We can overload Operator for only user-defined data types like objects or structure.
    » We can't overload any operator for built-in data types like int, char or float etc.
    » Operator Overloading can't change the precedence and associatively of Operators.







I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement