C++ overloading binary operators using friends

C++ Overloading binary operators using friends

As stated earlier, in c++ friend functions may be used in the place of member functions for overloading a binary operator, so the only difference being that a friend function requires 2(two arguments) to be explicitly passed to it, while a member function requires only one.

The program of complex number discussed in the previous section can be modified using a friend operator function as follows:

1). Replace the member declaration by the friend function declaration.

friend complex operator+(complex,complex);

2) Redefine the operator function as follows:

Complex operator+(complex a, complex b)
{
return complex (( a.x + b.x), (a.y+b.y));
}

In this case, the statement
C3 = C1+ C2;
Is equivalent to
C3 = operator+(C1 , C2);

In the most cases, we get the same results by the use of either a member function or a friend function. Why then an alternative is made available? There are certain conditions where we would like to use a friend function rather than a member function. For instance, consider the situation where you 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 in below,

A= B +2; (or A = B * 2;)
Where in the above example A and B are objects of the same class. it will work for a member function but the statement
A = 2 + B; (or A = 2 * B)

will not work. it 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. How?

it may be re-called that an object need to be used to invoke a friend function but can be passed as an argument. Thus, we can use a friend function or(method) with a built in type data as the left hand operand and an object as the right hand operand. Program no. 7.3 illustrates this, using the scalar multiplication of a vector. It also shows how to overload the input and output operators >> and<<.

[OVERLOADING OPERATORS USING FRIENDS] #include<iostream.h >
const size = 3;
class vector
{
int v[size];
public:
vector(); // constructs null vector
vector( int *x); // constructs vector from array
friend vector operator *( int a, vector b); //friend 1
friend vector operator *(vector b, int a); // friend 2
friend istream & operator >> (istream &, vector &);
friend ostream & operator << (ostream &,vector &);
};
vector : : vector()
{
for (int i=0; i < size; i++) v[i] =0;
}
vector : : vector(int * x)
{
for (int i =0; i< size; i++) v[i] = x[i];
}
vector operator *(int a, vector b)
{
vector c;
for (int i=0; i< size; i++) c.v[i] = a*b.v[i];
return c;
}
vector operator *(vector b, int a)
{
vector c;
for (int i=0; i< size; i++)
c.v[i] = b.v[i], *a;
return c;
}
istream & operator >> (istream &din, vector &b)
{
for(int i=0; i< size; i++)
din >> b.v[i];
return(din);
}
ostream & operator << (ostream &dout, vector &b)
{
dout << "(" << b.v[0];
for(int i=1; i < size; i++)
dout << ", " << b.v[i];
dout << ")";
return(dout);
}
int x[size] = {2,4,6};
int main()
{
vector m; // invokes constructor 1
vector n= x; // invokes constructor 2
std::cout<< "Enter elements of vector m" << "\n";
std::cin>> m; // invokes operator>>() function
std::cout<< "\n";
std::cout << "m= " << m << "\n"; // invokes operator <<()
vector p, q;
p = 2 * m; // invokes friend 1
q = n * 2; // invokes friend 2
std::cout<< "\n";
std::cout<< "p = " << p << "\n"; // invokes operator<<()
std::cout<< "q = " << q << "\n";
return 0;
}

Shown below in the output of program

Enter elements of vector
5 10 15
m = (5, 10, 15)
p = (10,20,30)
q = ( 4, 8, 12)

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise