Wednesday, January 6, 2016

OPERATOR OVERLOADING IN C++

OPERATOR 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.

Almost any operator can be overloaded in C++. However there are few operator which can not be overloaded.Operator that are not overloaded are follows

  • scope operator - ::
  • sizeof
  • member selector - .
  • member pointer selector - *
  • ternary operator - ?:

Operator Overloading Syntax



Implementing Operator Overloading

Operator overloading can be done by implementing a function which can be :
  1. Member Function
  2. Non-Member Function
  3. Friend Function
Operator overloading function can be a member function if the Left operand is an Object of that class, but if the Left operand is different, then Operator overloading function must be a non-member function.
Operator overloading function can be made friend function if it needs access to the private and protected members of class.
Overloading Unary Operators
The unary operators operate on a single operand and following are the examples of Unary operators:
The unary operators operate on the object for which they were called and normally, this operator appears on the left side of the object, as in !obj, -obj, and ++obj but sometime they can be used as postfix as well like obj++ or obj--.
Following example explain how minus (-) operator can be overloaded for prefix as well as postfix usage.
Example:
#include <iostream>
using namespace std;
 
class Distance
{
   private:
      int feet;             // 0 to infinite
      int inches;           // 0 to 12
   public:
      // required constructors
      Distance(){
         feet = 0;
         inches = 0;
      }
      Distance(int f, int i){
         feet = f;
         inches = i;
      }
      // method to display distance
      void displayDistance()
      {
         cout << "F: " << feet << " I:" << inches <<endl;
      }
      // overloaded minus (-) operator
      void operator- ()  
      {
         feet = -feet;
         inches = -inches;
         
      }
};
int main()
{
   Distance D1(11, 10), D2(-5, 11);
 
   -D1;                     // apply negation
   D1.displayDistance();    // display D1

   -D2;                     // apply negation
   D2.displayDistance();    // display D2

   return 0;
}
Output:
F: -11 I: -10
F: 5 I:-11
Binary Operator Overloading
The binary operators take two arguments and following are the examples of Binary operators. You use binary operators very frequently like addition (+) operator, subtraction (-) operator and division (/) operator.
Following example explains how addition (+) operator can be overloaded. Similar way, you can overload subtraction (-) and division (/) operators.
#include<iostream.h>
#include<conio.h>
 class complex
{
              int a,b;
    public:
              void getvalue()
              {
                 cout<<"Enter the value of Complex Numbers a,b:";
                 cin>>a>>b;
              }
              complex operator+(complex ob)
              {
                            complex t;
                            t.a=a+ob.a;
                            t.b=b+ob.b;
                            return(t);
              }
              complex operator-(complex ob)
              {
                            complex t;
                            t.a=a-ob.a;
                            t.b=b-ob.b;
                            return(t);
              }
              void display()
              {
                            cout<<a<<"+"<<b<<"i"<<"\n";
              }
};
void main()
{
   clrscr();
   complex obj1,obj2,result,result1;
   obj1.getvalue();
   obj2.getvalue();
   result = obj1+obj2;
   result1=obj1-obj2;
   cout<<"Input Values:\n";
   obj1.display();
   obj2.display();
   cout<<"Result:";
   result.display();
  result1.display();
   getch();
}
Output:

No comments:

Post a Comment