Friend Function

How would you create a friend function for cout and cin? It is for an assignment but I dont know how to start.
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.
What you can do though, is overload the << and >> operators for your own types.
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.
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
Oh thank you very much!
Topic archived. No new replies allowed.