Operator overloading

Why do we declare the << overloaded operator outside the class?

Depends,

The basic question is “Do you want conversions to be performed on the left-hand side parameter of an operator?”. If yes, use a free function. If no, use a class member.

For example, for operator+() for strings, we want conversions to be performed so we can say things like:

string a = “bar”;
string b = “foo” + a;
where a conversion is performed to turn the char * “foo” into an std::string. So, we make operator+() for strings into a free function.

So if you used the member function version, you COULD NOT have “foo” + a, but you could have a + “foo”