Question: 1 . Change the add function, so that it receives two AltMoney objects which it adds together and returns one object AltMoney as the result

1. Change the add function, so that it receives two AltMoney objects which it adds together
and returns one object AltMoney as the result of the addition.
Note that in the above version of the program, you have passed the object sum as call-byreference.
2. Add a new friend function, subtract, that computes the subtraction of one money from the
other. This function must receive by parameters two AltMoney objects which it will
subtract and return an AltMoney object with the result.
3. Make read_money a member function. Note that if you make read_money a member
function, then you can use it to directly initialize the dollars and cents of an AltMoney type
object directly.
#include
#include
using namespace std;
class AltMoney
{
public:
AltMoney();
AltMoney(int d, int c);
friend void add(AltMoney m1, AltMoney m2, AltMoney &sum);
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();
add(m1, m2, sum);
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 in the left for numbers less than 10
cout << cents << endl;
}
void add(AltMoney m1, AltMoney m2, AltMoney &sum)
{
int extra =0;
sum.cents = m1.cents + m2.cents;
if(sum.cents >=100)
{
sum.cents = sum.cents -100;
extra =1;
}
sum.dollars = m1.dollars + m2.dollars + extra;
}
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);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!