20-CS-122-001 Computer Science II Spring 2012
Operator Overloading

Virtual functions, classes, inheritance, lists, queues, stacks, applications

Prev      Next      Lectures

Overloading stream input and output

   Input Stream:

       istream &operator >>(istream &in, Fraction &frac) {
          long num, denom;
          char c;

          in >> num >> c;
          if (c == '/') in >> denom;
          else {
             ...
          }
          frac = Fraction(num, denom);
          return in;
       }

       Fraction f;

       cin >> f;

   Input Stream:

       istream &operator >>(istream &in, Fraction &frac) {
          frac.read(in);
          return in;
       }

       void Fraction::read(istream &in) {
          int num, denom;
          char c;

          in >> num >> c;
          if (c == '/') in >> denom;
          else {
              ...
          }
          *this = Fraction(num, denom);
       }