Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Modify the definition of the class Money shown in Display below so that the f

ID: 639806 • Letter: 1

Question

1. Modify the definition of the class Money shown in Display below so that the following
are added:
A) The operators < , <=, > , and >= have each been overloaded to apply to the type
Money .

B) The following member function has been added to the class definition. (We
show the function declaration as it should appear in the class definition. The
definition of the function itself will include the qualifier Money ::.)
const Money percent( int percentFigure) const;
//Returns a percentage of the money amount in the calling
//object. For example, if percentFigure is 10, then the value
//returned is 10% of the amount of money represented by the
//calling object.
For example, if purse is an object of type Money whose value represents the
amount $100.10, then the call
purse.percent(10);
returns 10% of $100.10; that is, it returns a value of type Money that represents
the amount $10.01.

I need this in c++ and please make sure it compiles and runs before you just throw a code out there.

***********************************************************************************************************************

1 #include <iostream>
2 #include <cstdlib>
3 #include <cmath>
4 using namespace std;
5 //Class for amounts of money in U.S. currency
6 class Money
7 {
8 public:
9 Money( );
10 Money( double amount);
11 Money( int theDollars, int theCents);
12 Money( int theDollars);
13 double getAmount( ) const;
14 int getDollars( ) const;
15 int getCents( ) const;
16 friend const Money operator +( const Money& amount1,
const Money& amount2);
17 friend const Money operator -( const Money& amount1,
const Money& amount2);
18 friend bool operator = =(const Money& amount1,
const Money& amount2);
19 friend const Money operator -( const Money& amount);
20 friend ostream& operator <<(ostream& outputStream,
const Money& amount);
21 friend istream& operator >>(istream& inputStream, Money& amount);
22 private:
23 //A negative amount is represented as negative dollars
//and negative cents. Negative $4.50 is represented as
//-4 and -50.
24 int dollars, cents;
25 int dollarsPart( double amount) const;
26 int centsPart( double amount) const;
27 int round( double number) const;
28 };
29 int main()
30 {
31 Money yourAmount, myAmount(10, 9);
32 cout << "Enter an amount of money: ";
33 cin >> yourAmount;
34 cout << "Your amount is " << yourAmount << endl;
35 cout << "My amount is " << myAmount << endl;
36
37 if (yourAmount == myAmount)
38 cout << "We have the same amounts. ";
39 else
40 cout << "One of us is richer. ";
41 Money ourAmount = yourAmount + myAmount;
42 cout << yourAmount << " + " << myAmount
43 << " equals " << ourAmount << endl;
44 Money diffAmount = yourAmount - myAmount;
45 cout << yourAmount << " - " << myAmount
46 << " equals " << diffAmount << endl;
47 return 0;
48 }
49 ostream& operator <<(ostream& outputStream, const Money& amount)
50 {
51 int absDollars = abs(amount.dollars);
52 int absCents = abs(amount.cents);
53 if (amount.dollars < 0 || amount.cents < 0)
54 //accounts for dollars == 0 or cents == 0
55 outputStream << "$-";
56 else
57 outputStream << '$';
58 outputStream << absDollars;
59 if (absCents >= 10)
60 outputStream << '.' << absCents;
61 else
62 outputStream << '.' << '0' << absCents;
63 return outputStream;
64 }
65
66 //Uses iostream and cstdlib:
67 istream& operator >>(istream& inputStream, Money& amount)
68 {
69 char dollarSign;
70 inputStream >> dollarSign; //hopefully
71 if (dollarSign != '$')
72 {
73 cout << "No dollar sign in Money input. ";
74 exit(1);
75 }
76 double amountAsDouble;
77 inputStream >> amountAsDouble;
78 amount.dollars = amount.dollarsPart(amountAsDouble);
79 amount.cents = amount.centsPart(amountAsDouble);
80 return inputStream;
81 }

Explanation / Answer


//Progam to demonstrate the class Money.
#include
#include
#include
#include
using namespace std;

//Class for amounts of money in U.S. Currency.
class Money
{
public:

friend Money operator +(const Money&amp; amount1, const Money&amp; amount2);
//Precondition: amount1 and amount2 have been given values.
//Returns the sum of the values of amount1 and amount2.

friend Money operator -(const Money&amp; amount1, const Money&amp; amount2);
//Precondition: amount1 and amount2 have been given values.
//Returns true if the amount1 and amount2 have the same value;
//otherwise, returns false.

friend Money operator -(const Money&amp; amount);
//Precondition: amount has been given a value.
//Returns the negative of the value of amount.

friend bool operator ==(const Money&amp; amount1, const Money&amp; amount2);
//Precondition: amount1 and amount2 have been given values.
//Returns true if amount1 and amount2 have the same value;
//otherwise, returns false.

Money(long dollars, int cents);
//Initializes the object so its value represents an amount with the
//dollars and cents given by the arguments. If the amount is negative,
//then both dollars and cents must be negative.

Money(long dollars);
//Initializes the object so its value represents $dollars.00.

Money();
//Initializes the object so its value represents {descr/string}.00.

double get_value() const;
//Precondition: The calling object has been given a constant value.
//Returns the amount of money recorded in the data of the calling object.

friend istream&amp; operator &gt;&gt;(istream&amp; ins, Money&amp; amount);
//Overloads the &gt;&gt; operator so it can be used to input values of type Money.
//Notation for inputting negative amounts is as in -.00.
//Precondition: If ins is a file input stream, then ins has already been
//connected to a file.

friend ostream&amp; operator &lt;&lt;(ostream&amp; outs, const Money&amp; amount);
//Overloads the &lt;&gt;.
//Precondition: c is one of the digits '0' through '9'.
//Returns the integer for the digit; for example, digit_to_int('3') returns 3.

int main()
{
Money amount;
ifstream in_stream;
ofstream out_stream;

in_stream.open(&quot;infile.dat&quot;);
if (in_stream.fail())
{
cout &lt;&lt; &quot;Input file opening failed.n&quot;;
exit(1);
}

out_stream.open(&quot;outfile.date&quot;);
if (out_stream.fail())
{
cout &lt;&gt; amount;

//cout &lt;&lt; &quot;Ln 87 &quot;&lt;&lt; amount &lt;&lt; endl &lt;&lt; endl; //temp

out_stream &lt;&lt; amount
&lt;&lt; &quot; copied from the file infile.dat.n&quot;;

cout &lt;&lt; amount
&lt;&lt; &quot; copied from the file infile.dat.n&quot;;

in_stream.close();
out_stream.close();

cout &lt;&gt;(istream&amp; ins, Money&amp; amount)
{
char one_char, decimal_point,
digit1, digit2; //digits for the amount of cents
long dollars;
int cents;
bool negative; //set to true if input is negative.

ins &gt;&gt; one_char;
if (one_char == '-')
{
negative = true;
ins &gt;&gt; one_char; //read '$'
}
else
{
negative = false;
//if input is legal, then '$'
}

ins &gt;&gt; dollars &gt;&gt; decimal_point &gt;&gt; digit1 &gt;&gt; digit2;

if ( one_char != '$' || decimal_point != '.'
|| !isdigit(digit1) || !isdigit(digit2))
{
cout &lt;&lt; &quot;Error illegal form for money inputn&quot;;
exit(1);
}

cents = digit_to_int(digit1)*10 + digit_to_int(digit2);

amount.all_cents = dollars*100 + cents;

if (negative)
amount.all_cents = -amount.all_cents;

return ins;
}

int digit_to_int(char c)
{
return ( static_cast(c) - static_cast('0'));
}

//Uses cstdlib and iostream:
ostream&amp; operator &lt;&lt;(ostream&amp; outs, const Money&amp; amount)
{
long positive_cents, dollars, cents;
positive_cents = labs(amount.all_cents);
dollars = positive_cents/100;
cents = positive_cents%100;

if (amount.all_cents &lt; 0)
outs &lt;&lt; &quot;-$&quot; &lt;&lt; dollars &lt;&lt; '.';
else
outs &lt;&lt; &quot;$&quot; &lt;&lt; dollars &lt;&lt; '.';

if (cents &lt; 10)

outs &lt;&lt; '0';
outs &lt;&lt; cents;

return outs;
}

Money operator +(const Money&amp; amount1, const Money&amp; amount2)
{
Money temp;
temp.all_cents = amount1.all_cents + amount2.all_cents;
return temp;
}

Money operator -(const Money&amp; amount1, const Money&amp; amount2)
{
Money temp;
temp.all_cents = amount1.all_cents - amount2.all_cents;
return temp;
}

Money operator -(const Money&amp; amount)
{
Money temp;
temp.all_cents = -amount.all_cents;
return temp;
}

bool operator ==(const Money&amp; amount1, const Money&amp; amount2)
{
return (amount1.all_cents == amount2.all_cents);
}

Money::Money(long dollars, int cents)
{
if (dollars*cents &lt; 0) //If one is negative and one is positive
{
cout &lt;&lt; &quot;Illegal values for dollars and cents.n&quot;;
exit(1);
}
all_cents = dollars*100 + cents;
}

Money::Money(long dollars) : all_cents(dollars*100)
{
//Body intentionally blank.
}

Money::Money() : all_cents(0)
{
//Body intentionally blank.
}
double Money::get_value() const
{
return (all_cents*0.01);
}