Complete Display 11.5. Create a complete application. Here is the Display: //Pro
ID: 3537060 • Letter: C
Question
Complete Display 11.5. Create a complete application. Here is the Display:
//Program to demonstrate the class Money. (This is an improved version of
// the class Money that we gave in Display 11.3 and rewrote in Display 11.4.)
#include
<iostream>
#include
<cstdlib>
#include
<cctype>
using
namespace std;
//Class for amounts of money in U.S. currency.
class
Money
{
public
:
friend Money operator +(const Money& amount1, const Money& amount2);
//Precondition: amount1 and amount2 have been given values.
//Returns the sum of the values of amount1 and amount2.
friend bool operator ==(const Money& amount1, const Money& amount2);
//Precondition: amount1 and amount2 have been given values.
//Returns true if amount1 and amount2 have the same value;
//otherwise, return 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 ( ) ;
double get_value( ) const;
//Initializes the object so its value represents $0.00
void input(istream& ins);
//Precondition: If ins is a file input stream, then ins has already been
//connected to a file. An amount of money, including a dollar sign, has been
//entered in the input stream ins. Notation for negative amounts is -$100.00.
//Postcondition: The value of the calling object has been set to
//the amount of money read from the input stream ins.
void output(ostream& outs) const;
//Precondition: If outs is a file output stream, then outs has already been
//connect to a file.
//Postcondition: A dollar sign and the amount of money recorded
//in the calling object have been sent to the output stream outs.
private
:
long all_cents;
};
int
main( )
{
Money cost(1, 50), tax(0, 15), total;
total = cost + tax;
cout<<
"cost = ";
cost.output(cout);
cout<< endl;
cout<<
"tax = ";
tax.output(cout);
cout<<endl;
cout<<
"total bill = " ;
total.output(cout);
cout << endl;
if (cost == tax)
cout<<
"Move to another state. ";
else cout<< "Things seem normal. ";
return 0;
}
Money
operator +(const Money& amount1, const Money& amount2)
{
Money temp;
temp.all_cents == amount1.all_cents + amount2.all_cents;
return temp;
}
bool
operator ==(const Money& amount1, const Money& amount2)
{
return(amount1.all_cents == amount2.all_cents);
%u3000
}
%u3000
%u3000
Explanation / Answer
#include<iostream>
#include<cstdlib>
#include<cctype>
using namespace std;
class Money
{
public:
friend Money operator +(const Money& amount1, const Money& amount2);
//Precondition: amount1 and amount2 have been given values.
//Returns the sum of the values of amount1 and amount2.
friend bool operator ==(const Money& amount1, const Money& amount2);
//Precondition: amount1 and amount2 have been given values.
//Returns true if amount1 and amount2 have the same value;
//otherwise, return false.
Money(long dollars, int cents)
{
all_cents = 100*dollars + 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)
{
all_cents = 100*dollars;
}
//Initializes the object so its value represents $dollars.00.
Money ( )
{
all_cents = 0;
}
double get_value( ) const
{
return all_cents;
}
//Initializes the object so its value represents $0.00
void input(istream& ins);
//Precondition: If ins is a file input stream, then ins has already been
//connected to a file. An amount of money, including a dollar sign, has been
//entered in the input stream ins. Notation for negative amounts is -$100.00.
//Postcondition: The value of the calling object has been set to
//the amount of money read from the input stream ins.
void output(ostream& outs) const
{
outs << " $ " << (all_cents/100) << " . " << ( all_cents - (all_cents/100)*100) << endl;
}
//Precondition: If outs is a file output stream, then outs has already been
//connect to a file.
//Postcondition: A dollar sign and the amount of money recorded
//in the calling object have been sent to the output stream outs.
private:
long all_cents;
};
int main( )
{
Money cost(1, 50), tax(0, 15), total;
total = cost + tax;
cout<<"cost = ";
cost.output(cout);
cout<< endl;
cout<<"tax = ";
tax.output(cout);
cout<<endl;
cout<<"total bill = " ;
total.output(cout);
cout << endl;
if (cost == tax)
cout<<"Move to another state. ";
else cout<< "Things seem normal. ";
return 0;
}
Money operator+(const Money& amount1, const Money& amount2)
{
Money temp;
temp.all_cents = amount1.all_cents + amount2.all_cents;
return temp;
}
bool operator ==(const Money& amount1, const Money& amount2)
{
return(amount1.all_cents == amount2.all_cents);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.