Step 1: Change the program with add function as a friend function. // This progr
ID: 673198 • Letter: S
Question
Step 1: Change the program with add function as a friend function.
// This program adds money of two different people
#include<iostream>
#include<cstdlib>
using namespace std;
class AltMoney
{
public:
AltMoney();
AltMoney(int d, int c);
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 2: Make function add of type AltMoney. This function now computes the sum of dollars and cents and returns it as AltMoney. Note that in the above version of the program, you have passed the object sum as call_by_reference.
Step 3: add a new friend function, subtract, that computes the subtraction of one money from the other.
Step 4: 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.
Explanation / Answer
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";
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);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.