Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Input (>>) and Output (<<) Operator Overloading

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi.
I need some help in overloading ( << and >> ).

I need to be able to overload >> to accept input of rational(fraction) numbers.

cin >> n;

thank you
 
Write them like regular functions named &quot;operator<<&quot; and &quot;operator>>&quot;.

operator<< should take an &quot;ostream &&quot; (the type of cout and other output streams) as its first argument, and it should take either &quot;YourClass&quot; or &quot;const YourClass &&quot; as its second argument. It should return an &quot;ostream &&quot;, which should just be its first argument (this allows output commands to be chained).

operator>> should take an &quot;istream &&quot; (the type of cout and other output streams) as its first argument, and it should take &quot;YourClass &&quot; as its second argument. It should return an &quot;istream &&quot;, which should just be its first argument (this allows input commands to be chained).

You may need to declare the input operator (or both) to be friends of your class in orger to access its private data.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top