Activity 8-3 - Overloading Operators >> and <<



The input and output operators >> and << can be overloaded like other operators.  In such a case, the value returned must be the stream and the type for the value returned must have the & symbol added to the end of the type name.   Here is the declaration:

declaration:
class Class_Name
{
    public:
          ...
         friend istream& operator >>(istream& parameter_1, Class_Name& parameter_2);
         friend ostream& operator <<(ostream& parameter_3, const Class_Name& parameter_4);
        ...

definition:
istream& operator >>(istream& parameter_1, Class_Name& parameter_2)
{
     ...
}

ostream& operator <<(istream& parameter_3, Class_Name& parameter_4)
{
     ...
}

Following is a new version of the program P82.cpp, in which we read the two input values from an input file, in_file.dat, and writes the results into an output file, out_file.dat.

// P83.cpp - This program adds money of two different people
// It reads the amounts for two people from an
// input file in_file.dat and writes the result into a file out_file.dat

#include<iostream>
#include<cstdlib>
#include<fstream>
using namespace std;

class AltMoney
{
    public:
        AltMoney();
        friend void read_money(istream& ins, AltMoney& m);
        friend AltMoney operator +(AltMoney m1, AltMoney m2);
        friend void write_money(ofstream& ous, AltMoney m);
    private:
        int dollars;
        int cents;
};

void read_money(istream& ins, AltMoney& m);
void get_streams(ifstream& ins, ofstream& ous);
void write_money(ofstream& ous, AltMoney m);

int main( )
{
     ifstream ins;
    ofstream ous;
     AltMoney m1, m2, sum;

     get_streams(ins, ous);

     read_money(ins, m1);
     ous << "The first money is:";
     write_money(ous, m1);

     read_money(ins, m2);
     ous << "The second money is:";
     write_money(ous, m2);

     sum = m1+m2;
     ous << "The sum is:";
     write_money(ous, sum);

     ins.close();
     ous.close();

     return 0;
}

AltMoney::AltMoney()
{
}

void write_money(ofstream& ous, AltMoney m)
{
     ous << "$" << m.dollars << ".";
     if(m.cents <= 9)
         ous << "0";  //to display a 0 on the left for numbers less than 10
     ous << m.cents << endl;
}

AltMoney operator +(AltMoney m1, AltMoney m2)
{
     AltMoney temp;
     int extra = 0;
     temp.cents = m1.cents + m2.cents;
     if(temp.cents >=100){
         temp.cents = temp.cents - 100;
         extra = 1;
      }
      temp.dollars = m1.dollars + m2.dollars + extra;

      return temp;
}

void read_money(istream& ins, AltMoney& m)
{
     int d, c;
     ins >> d;
     ins >> c;
     if( d < 0 || c < 0)
     {
            cout << "Invalid dollars and cents, negative values\n";
            exit(1);
     }
     m.dollars = d;
     m.cents = c;
}

void get_streams(ifstream& ins, ofstream& ous)
{

    ins.open("in_file.dat");
    if(ins.fail())
   {
       cout << "Failed to open the input file. \n";
       exit(1);
    }

    ous.open("out_file.dat");
    if(ous.fail())
   {
       cout << "Failed to open the output file. \n";
       exit(1);
    }
}

We will overload the >> so that writing an object of type AltMoney can be done using >>.   In order to overload >> to read an object of type AltMoney, you need to make the following changes in the program.

1) In the class AltMoney, replace the read_money function with the operator >> as described below:
class AltMoney
{
    public:
        AltMoney();
        friend istream& operator >>(istream& ins, AltMoney& m);
        friend AltMoney operator +(AltMoney m1, AltMoney m2);
        friend void write_money(ofstream& ous, AltMoney m);
    private:
        int dollars;
        int cents;
};

2) Prototype (comes before the main function)
istream& operator >>(istream& ins, AltMoney& m);

3) In the main:
     get_streams(ins, ous);

     ins >> m1;
     ous << "The first money is:";
     write_money(ous, m1);

     ins >> m2;
     ous << "The second money is:";

4) replace read_money function with:
istream& operator >>(istream& ins, AltMoney& m)
{
  int d, c;
     ins >> d;
     ins >> c;
     if( d < 0 || c < 0)
     {
            cout << "Invalid dollars and cents, negative values\n";
            exit(1);
     }
     m.dollars = d;
     m.cents = c;
    return ins;
}

Exercise 8.3
In program P83.cpp, make the above changes, save the program as ex83.cpp, compile and run the program and make sure the correct output is produced.  Your input file, in_file.dat, will have four integer values representing the dollars and cents part of the first amount and the dollars and cents part of the second amount.

Example: for input $50.34 and $86.73, you may have:
50 34
86 73

Once your program has successfully compiled and run, overload the write_money function as well.