Why we always need a friend function to overload (>>) in stream or (<<) out stream operator.
Asked
Active
Viewed 451 times
2 Answers
0
It doesn't have to be a friend, unless the object contains private data members that cannot be accessed otherwise.
For example, if the class happens to have getters and setters for all members, the operator can be implemented using those. In that case it doesn't have to be a friend.

Bo Persson
- 90,663
- 31
- 146
- 203
-1
Insertion operator << is overloaded with friend function. The overloaded operator allow us to display contents of object directly using cout statement. Extraction operator is overloaded after execution of cin statement the operator is invoked. Refer this for more detailed clarification.