Using the public interface of Employee in writing operator<< works, but there are more flexible choices.
- Write a public member of Employee - string
toString() then write the overloaded operator<< like
this:
ostream& operator<<(ostream& os, const Employee& e) { os << e.toString(); return os; }
- The Employee class can declare the non-member function
operator<< to be a friend function.
Declaring a function to be a friend function of Employee means the function can access all members including private members of any Employee parameter passed to it.
The first choice has superior benefits for opertor<< in case subclasses of Employee are created later (such as a Manager subclass).