Saturday, August 18, 2007

Operator Overloading using Friend Functions

In the article Introduction to Operator Overloading in C++, we discussed that there are two methods by which operators can be overloaded, one using the member function and the other by using friend functions.

There are some differences between the two methods though, as well as there are advantages for using friend functions to overload operators over member functions.

In this article we’ll be overloading the simplest operators – and + using friend function. Previously we have seen that we need to accept only one argument explicitly for binary operators and the other is passed implicitly using the ‘this’ pointer.

From the article Friend Functions of a Class, we know that as friend functions are not members of a class, they don’t have a ‘this’ pointer. So how the operands are are passed in this case?

Simple, all the operands are passed explicitly to the friend operator functions.

There are other differences also but for this article (to overload + and – operators) this is enough.

Here is the program:

  // Using friend functions to
  // overload addition and subtarction 
  // operators
  #include <iostream.h>

  class myclass
    int a;
    int b;

    myclass(int x,int y){a=x;b=y;}
    void show()

    // these are friend operator functions
    // NOTE: Both the operans will be be
    // passed explicitely.
    // operand to the left of the operator
    // will be passed as the first argument
    // and operand to the right as the second
    // argument
    friend myclass operator+(myclass,myclass);
    friend myclass operator-(myclass,myclass);


  myclass operator+(myclass ob1,myclass ob2)
    myclass temp;

    temp.a = ob1.a + ob2.a;
    temp.b = ob1.b + ob2.b;

    return temp;

  myclass operator-(myclass ob1,myclass ob2)
    myclass temp;

    temp.a = ob1.a - ob2.a;
    temp.b = ob1.b - ob2.b;

    return temp;

  void main()
    myclass a(10,20);
    myclass b(100,200);


Related Articles:


  1. An Excellent piece of work. You have taken lot of pains to explain C++ freind functions in a wonderful fashion. However, I would like to request you, if you could guide me as to "how two matrices can be added using operator overloading in c++", I would be really grateful.

    Alternatively, you could also give me some links as to where I could find a solution to my problem of "adding two matrices using Operator Overloading in c++".


    Ravinder Murthy

  2. Divide this into two, i've already discussed adding two matrices (use search) and see my other article on operator overloading (again use search if you don't find it).


  3. Ben Bradford3:36 PM

    These are excellent tutorials and they have been very helpful and your efforts are appreciated.

    However, I am struggling to see the advantage of using friend operator overloading.

    How does the above code differ from simply having

    myclass& operator+(const myclass& m2)

    declared inside of myclass?

    Thanks for your help

  4. Hi Ben Bradford,

    With binary operators overloaded through member functions you can't write the following expression:

    obj = 10 + obj2;

    Can you?

    Because it's the object that makes the call to the overloaded function while here 10 doesn't and cannot have that. But as overloading with friend functions require us to pass both the operands, above is possible. For that you'll have to have two overloaded function.


    1> operator+(myclass ob, int x)
    2> operator+(int x, myclass ob)

    Hope this helps!

  5. arvind guptha ..... u r super man.... thank u soomuch....why should we use freind a....and where it is necessary .... i'm slear now...

    i would like to add one more point here.....

    when we go for unary minus


    we cannot call minus operator directly .
    so we need to go for friend function..

    am i correct please reply me....


  6. Anonymous10:53 PM


  7. Anonymous8:18 PM

    Can u explain the concept of virtual function in brief?

  8. Anonymous12:05 PM

    pls post the output also, that will help us to deep understand .

  9. Anonymous6:23 PM

    Your coding style is not consistent. Especially spaces around operators are sometimes missing. It makes the code look messy.

  10. Anonymous5:45 PM

    pls post the output

  11. Anonymous7:54 PM

    amazing.......i really understood the usage of friend in operator overloading.

  12. The o/p is 110,220.if u give a=a-b; the its -90,-180.


You are free to comment anything, although you can comment as 'Anonymous' it is strongly recommended that you supply your name. Thank You.

Please don't use abusive language.