OOPortal
J2EEOnline RationalDB
prev next prev next
  Course navigation
 
Lesson 12
Objective
Overloading I/O operators
Overload operator << to output playing card
   
Overload the operator << so it outputs a playing card in a nicely formatted style.
Let us write these functions for the type rational:
class rational {
public:
   friend ostream&
     operator<<(ostream& out, rational x);
   friend istream&
     operator>>(istream& in, rational& x)
.....
private:
   long  a, q;
};

ostream& operator<<(ostream& out, rational x)
{
   return (out << x.a << " / " << x.q << '\t');
}
Overloading Insertion Operator Exercise
Click the Exercise link below to overload the operator << so it outputs a playing card in a nicely formatted style.
Overloading Insertion Operator Exercise
  Course navigation