/    /  CPP- Overloading Binary Operators Using friends

Overloading Binary Operators Using friends

 

The friend functions are more useful in operator overload. They offer more flexibility, which is not provided by the class member functions. The difference between the member function and the friend function is that the member function takes arguments explicitly. On the contrary, the friendly function requires that the parameters be explicitly passed.

 

Example:

class vector
{ 
 int v[5];
 public:
 vector();
 vector(int *x);
 friend vector operator+(vector M, vector N);
};
vector operator+(vector M, vector N)
{
int i;
vector A;
for(i=0;i<5;i++)
A.v[i] = M.v[i]+N.v[i];
return A;
}