In: Computer Science
First make the 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.
#include<iostream>
#include<cstdlib>
using namespace std;
class AltMoney
{
public:
AltMoney();
AltMoney(int d, int c);
friend AltMoney 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;
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();
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 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);
}
}
//Modified program
#include<iostream>
#include<cstdlib>
using namespace std;
class AltMoney
{
public:
AltMoney();
AltMoney(int d, int c);
friend AltMoney operator +(AltMoney m1, AltMoney m2);
friend double operator %(AltMoney income , AltMoney
expenditure);
bool operator >(AltMoney m);
bool operator <(AltMoney m);
bool operator ==(AltMoney m);
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 = 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 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;
}
bool AltMoney::operator >(AltMoney m){
if(this->dollars>m.dollars)return true;
if(this->dollars==m.dollars)
if(this->cents>m.cents)return
true;
return false;
}
bool AltMoney::operator <(AltMoney m){
if(this->dollars<m.dollars)return true;
if(this->dollars==m.dollars)
if(this->cents<m.cents)return
true;
return false;
}
bool AltMoney::operator ==(AltMoney m){
return (this->dollars==m.dollars &&
this->cents==m.cents);
}
double operator %(AltMoney income , AltMoney expenditure){
if(income>expenditure){
return
0.05*((income.dollars*100+income.cents)-(expenditure.dollars*100+expenditure.cents));
}
else if(income<expenditure){
return
0.02*((expenditure.dollars*100+expenditure.cents)-(income.dollars*100+income.cents));
}
return 0;
}
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);
}
}