|
||
|
Lesson 13
Objective
|
Overloading I/O operators Overload input operator >> for card class. |
|
|
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);
}
Click the Exercise button to overload the input operator for the card class.
Overloading Stream Extraction Operator |
||
|
|
||