Apr 23, 2012 at 7:04pm UTC
How would you create a friend function for cout and cin? It is for an assignment but I dont know how to start.
Apr 23, 2012 at 7:15pm UTC
You can not create a friend function for standard classes cout and cin because their realization in fact is hidden. You can declare a friend function for your class.
Apr 23, 2012 at 7:16pm UTC
What you can do though, is overload the << and >> operators for your own types.
Apr 23, 2012 at 7:18pm UTC
Thank you for your quick response, My instructor want us to to this:
Provide a friend function << for cout
Provide a friend function >> for cin
No idea how or where to start.
Apr 23, 2012 at 7:22pm UTC
Then he means that the operators are supposed to be friends of your own class, and should be used to write to cout/ read from cin.
For that, you need to provide the functions
std::ostream& operator <<(std::ostream&, const YourClass&);
and
std::istream& operator >>(std::istream&, YourClass&);
Both should be declared friends of YourClass (replace YourClass with the name of your class, obviously).
cout is an instance of ostream, cin is an instance of istream, in case you didn't know.
Last edited on Apr 23, 2012 at 7:23pm UTC