cout << -- an operator not like the others.
And you thought you would understand the operator business. After all, we did implement the assignment (operator=) -- what is different if we write cout << c1 << endl; ??
Remember that with the construct on the left, we are calling a member function of c2 with a “reference to object c1” as its parameter.
In the same way, we would call a member function of the object cout with a parameter of “reference to object c1”, which is of class “cube”.
Wait a minute -- does cout have a member function “operator<< (const cube &)”?
No, of course not. We made the cube class, there is no way how cout could know about our cube class.
Also, this would not be what we want. We want the knowledge how to output our cube class to live with the cube class, not with cout. What do we do?
Ok, now we are actually calling the cube member function with a parameter of type “reference to the object cout”. But this doesn’t look right to the eye.
And we would have to write endl << cout in a separate statement. Not good.
The solution is to have a friend.