Why should overloading of stream operators(<<,>>) should be kept as friends rather than making them members of the class?
Members of what class? What is the type of the left-hand operand?
They don't have to be friend
, though, unless there's a need to access otherwise unaccessible private data.
When you overload a binary operator as a member function of a class the overload is used when the first operand is of the class type.
For stream operators, the first operand is the stream and not (usually) the custom class.
For this reason overloaded stream operators for custom classes which are designed to be used in the conventional manner can't be member functions of the custom class, they must be free functions.
(I'm assuming that the stream classes are not open to be changed; if they were you could add member functions to stream classes to cope with additional custom types but this would usually be undesirable from a dependency point of view.)
Whether or not they are friends should depend on whether they need access to non-public members of the class.
So you can say:
some_stream << my_class;
Note that the definition of member operators makes the left hand side the class it self. e.g.:
my_class << some_stream;
Which is not how the standard streams are supposed to work.