Thursday, January 7, 2016

OPERATOR FUNCTION AS FRIEND FUNCTION IN OPERATOR OVERLOADING

     Operator functions must be either member function or friend function. a basic difference between them is that a friend function will have only one argument for unary operators and two for binary operators, while a member function has no arguments for unary operators and one for binary operators. This is because the object use to invoke the member function is passed implicitly and therefore is available for the member function. Arguments may be passed either by value or by reference. 

Unary Operator Overloading using Operator Function as Friend


#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
      friend void operator- (Distance & D1)  
      {
         feet = -D1.feet;
         inches = -D1.inches;
      }
};
int main()
{
   Distance D1(11, 10),;
 
   -D1;                     // apply negation
   D1.displayDistance();    // display D1


   return 0;
}
Output:
F: -11 I: -10

Binary Operator Overloading using Operator Function as Friend

#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;
              }
              friend complex operator+(complex p, complex q)
              {
                            return complex((p.a+q.a),(p.b+q.b));
              }
              friend complex operator-(complex ob)
              {
                         return complex((p.a-q.a),(p.b-q.b));
              }
              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:

In most cases, we will get the same results by the use of either a friend function or a member function. why then an alternative is made available? There are certain situations where we would like to use a friend function rather than a member function. For instance, consider a situation where we need to use two different types of operands for a binary operator, say one an object and another a built-in type data as shown below
A= B+2; or A=B*2;
Where A and B are objects of the same class. This will work for a member function but the statement 
A= 2+b; or A=2*B;
will not work. This is because the left-hand operand which is responsible for invoking the member function should be an object of the same class. However friend function allows both approaches.

No comments:

Post a Comment