Activity 8-2 - Overloading Operators



In the program P81.cpp and P81a.cpp,  you learned to define friend functions to access the variable members of a class.  In those programs, you used a function called add to do the addition of money (dollars and cents), and similarly did the subtraction of money using the subtract function.   Since the addition or subtraction of money required two components, it was different from an arithmetic add operation.  In C++, we can overload the add operator, ''+", to do different types of addition, for example: an the addition of money in the previous programs (dollars + dollars and cents + cents).  By using function overloading, we will give the power of function add to operator "+", which is also defined in the class definition.

Before we give an example of overloading operators, let's look at a new version of P81a.cpp in which the add function is defined as a friend and has the new type of AltMoney.

// P82.cpp - This program adds money of two different people
#include<iostream>
#include<cstdlib>
using namespace std;

class AltMoney
{
    public:
        AltMoney();
        AltMoney(int d, int c);

        friend AltMoney add(AltMoney m1, AltMoney m2);
        void display_money( );
    private:
        int dollars;
        int cents;
};

void read_money(int& d, int& c);

int main( )
{
     int d, c;
     AltMoney m1, m2, sum;

     sum = AltMoney(0,0);

     read_money(d, c);
     m1 = AltMoney(d,c);
     cout << "The first money is:";
     m1.display_money();

     read_money(d, c);
     m2 = AltMoney(d,c);
     cout << "The second money is:";
     m2.display_money();

     sum = add(m1,m2);
     cout << "The sum is:";
     sum.display_money();

     return 0;
}

AltMoney::AltMoney()
{
}

AltMoney::AltMoney(int d, int c)
{
       dollars = d;
       cents = c;
}

void AltMoney::display_money()
{
     cout << "$" << dollars << ".";
     if(cents <= 9)
         cout << "0";  //to display a 0 on the left for numbers less than 10
     cout << cents << endl;
}

AltMoney add(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(int& d, int& c)
{
     cout << "Enter dollar \n";
     cin >> d;
     cout << "Enter cents \n";
     cin >> c;
     if( d < 0 || c < 0)
     {
            cout << "Invalid dollars and cents, negative values\n";
            exit(1);
      }
}

In this program the add is defined as a friend and has the type AltMoney as well.

Now, let's overload the "+" operator so that is does what the function add is doing.

Here are the changes that you need to make: (red font)

1) In class AltMoney:
class AltMoney
{
    public:
        AltMoney();
        AltMoney(int d, int c);

        friend AltMoney operator +(AltMoney m1, AltMoney m2);
        void display_money( );
    private:
        int dollars;
        int cents;
};

2) In the main:

     sum = m1+m2;

3) In the function definition
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;
}

By making these changes, you can now add two objects of type AltMoney.  Note that you may overload "+" in another class definition.  Depending on how you have defined it, "+" operation may have different meanings in such cases.

Exercise 8.2
First make the above changes in P82.cpp and call the new program ex82.cpp.  Compile and run the program and make sure it produces the correct results.  Here is what you need to do for the exercise:

    Overload the % operator such that every time you use it, it takes two objects of type AltMoney as its arguments and returns:
            a) 5% of the difference between the income and expenditure, if income is larger than the expenditure
            b) -2% if the the expenditure is larger than the income.
            c) 0 if the expenditure is the same as income
Note that, by doing this, you are required to overload the greater than sign (>), the smaller than sign (<), and the == sign.