Given the class:
struct employee { string name; string ID; string phone; string department; };
How does the following function work?
ostream &operator<<(ostream &s, employee &o) { s << o.name << endl; s << "Emp#: " << o.ID << endl; s << "Dept: " << o.department << endl; s << "Phone: " << o.phone << endl; return s; }
cout << e; produces formatted output for the given employee e .
Output Example:
Alex Johnson Emp#: 5719 Dept: Repair Phone: 555-0174
I cannot understand how the ostream function works. How does it get the parameter "ostream & s"? How does it overload "<<the operator and how <the operator’s work? How can it be used to write all the information about the employee? Can someone answer these questions in the conditions of a layman?
source share