C++ | Operator Overloading | Question 4
Which of the following operators should be preferred to overload as a global function rather than a member method?
(A) Postfix ++
(B) Comparison Operator
(C) Insertion Operator <<
Explanation: cout is an object of ostream class which is a compiler defined class.
When we do “cout << obj" where obj is an object of our class, the compiler first looks for an operator function in ostream, then it looks for a global function.
One way to overload insertion operator is to modify ostream class which may not be a good idea. So we make a global method. Following is an example.
using namespace std;
Complex(int r = 0, int i =0)
real = r;
imag = i;
friend ostream & operator << (ostream &out, const Complex &c);
ostream & operator << (ostream &out, const Complex &c)
out << c.real;
out << "+i" << c.imag;
Complex c1(10, 15);
cout << c1;
Quiz of this Question