Finally, consider the class Pair:
1 class Pair 2 { 3 int x; 4 int y; 5 public: 6 Pair() : x(0), y(0) 7 { } 8 9 Pair(int x, int y) : x(x), y(y) 10 { } 11 12 int getX() const; 13 int getY() const; 14 string toString() const; 15 16 friend ostream& operator<<(ostream& os, const Pair& pr); 17 18 };
We really don't need to make this operator<< be a friend, since it will use the public method toString().
A friend function may be declared inside a class definition, even though it is not a member function of the class.
If a non-member function is not a friend, it must be declared outside the class.