OOPortal OOPortal

Operator Overloading  «Prev  Next»
Lesson 13Overloading I/O operators
Objective Overload input operator >> for card class.

Overloading input Operator in C++

When overloading the operator >> to produce input to a user-defined type, the typical form is:
istream& operator>>(istream& p, user-defined type& x)

If the function needs access to private members of x, it must be made a friend of its class. A key point is to make x a reference parameter so that its value can be modified. To do this for the rational class would require placing a friend declaration for this operator in the rational class and providing its function definition.

istream& operator>>(istream& in, rational& x){
   return (in >> x.a >> x.q);

Overloading Stream Extraction Operator - Exercise

Click the Exercise link below to overload the input operator for the card class.
Overloading Stream Extraction Operator - Exercise