Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include using namespace std; class AltMoney { public: AltMoney(); AltMoney(int d, int c); friend AltMoney add(AltMoney m1, AltMoney m2); void display_money( ); private: int

#include #include 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 "; cin >> d; cout << "Enter cents "; cin >> c; if( d < 0 || c < 0) { cout << "Invalid dollars and cents, negative values "; 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; }

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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Inductive Databases And Constraint Based Data Mining

Authors: Saso Dzeroski ,Bart Goethals ,Pance Panov

2010th Edition

1489982175, 978-1489982179

More Books

Students also viewed these Databases questions

Question

13-1 How does building new systems produce organizational change?

Answered: 1 week ago

Question

13-4 What are alternative methods for building information systems?

Answered: 1 week ago