friend operator : overload with friend function « Operator Overloading « C++ Tutorial






#include <iostream.h>

class Num
{
       friend Num operator+(Num ob, int i);
       friend Num operator+(int i, Num ob);
       int count;
public:
       Num(int cc=0){
           count=cc;
    }
       Num& operator=(int i);
    void Show(){
        cout <<count<<endl;
    }
};

Num& Num::operator=(int i){  
    count=i;  
    return *this;
}

Num operator+(Num ob, int i) // This handles ob+int.
{  
    Num temp;  
    temp.count=ob.count+i;  
    return temp;
}

Num operator+(int i, Num ob) // This handles int+ob.
{  
    Num temp;  
    temp.count=ob.count+i;  
    return temp;
}

main(void)
{
       Num obj;
       obj=10;
       obj.Show();     
       obj=10+obj;     
       obj.Show();     
       obj=obj+12;     
       obj.Show();     
       return 0;
}
10
20
32








10.17.overload with friend function
10.17.1.friend operator