In: Computer Science
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.
// C++ program to implement overloaded operators using friend functions
#include <iostream>
#include<cstdlib>
using namespace std;
class AltMoney
{
public:
AltMoney();
AltMoney(int d, int c);
friend AltMoney operator +(AltMoney m1, AltMoney m2);
// new overloaded operators
friend AltMoney operator %(AltMoney income, AltMoney expenditure);
friend bool operator>(AltMoney m1, AltMoney m2);
friend bool operator<(AltMoney m1, AltMoney m2);
friend bool operator==(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, percent;
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 = m1+m2;
cout << "The sum is:";
sum.display_money();
percent = m1%m2;
cout<<"The percent is : ";
percent.display_money();
return 0;
}
AltMoney::AltMoney()
{
dollars = 0;
cents = 0;
}
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 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(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);
}
}
// returns a percentage of difference between income and expenditure
AltMoney operator %(AltMoney income, AltMoney expenditure)
{
AltMoney temp;
int income_in_cents = income.dollars*100 + income.cents; // get the income in cents
int exp_in_cents = expenditure.dollars*100 + expenditure.cents; // get the expenditure in cents
int diff_in_cents;
if(income > expenditure)
{
diff_in_cents = income_in_cents - exp_in_cents;
int req_result = diff_in_cents*0.05;
temp.dollars = req_result/100;
temp.cents = req_result%100;
}
else if(income < expenditure)
{
diff_in_cents = exp_in_cents - income_in_cents;
int req_result = diff_in_cents*0.02;
temp.dollars = req_result/100;
temp.cents = req_result%100;
}
return temp;
}
// returns true if m1 is greater than m2 else false
bool operator>(AltMoney m1, AltMoney m2)
{
if(m1.dollars > m2.dollars)
return true;
else if(m1.dollars < m2.dollars)
return false;
else
{
if(m1.cents > m2.cents)
return true;
else
return false;
}
}
// returns true if m1 is less than m2 else false
bool operator<(AltMoney m1, AltMoney m2)
{
if(m1.dollars < m2.dollars) // if dollars of m1 < dollars of m2 , then return true
return true;
else if(m1.dollars > m2.dollars) // check if dollars of m1 > dollars of m2, then return false
return false;
else // check the cents of m1 and m2 if dollars are equal
{
if(m1.cents < m2.cents)
return true;
else
return false;
}
}
// returns true if m1 is equal to m2 else false
bool operator==(AltMoney m1, AltMoney m2)
{
return((m1.dollars == m2.dollars) && (m1.cents == m2.cents)); // check if both dollars and cents of m1 and m2 are equal
}
//end of program
Output: