Question: (Using C++): Complete the following program: Money.h file: #pragma once // Class for amounts of money in U.S. currency. // Name // Date #include using

(Using C++):

Complete the following program:

Money.h file:

#pragma once // Class for amounts of money in U.S. currency. // Name // Date #include using std::istream; using std::ostream; class Money { public: // initalizes dollars and cents to 0 Money(); // accepts the amount as a double and sets the dollars and cents // e.g., if amount = 10.50, then this constructor sets dollars to 10 and cents to 20 // uses the dollarsPart() and centsPart() private functions Money(double amount); // initalizes the dollars and cents to theDollars and theCents respectively // produces an error messages if the money data is inconsistent (one of the parts is negative) // and exits the application Money(int theDollars, int theCents); // initalizes the dollars to theDollars and cents to 0 Money(int theDollars); // returns the amount as a double // (e.g., dollars = 10, cents=50, the function returns 10.50) double getAmount() const; // returns the dollars int getDollars() const; // returns the cents int getCents() const; // overloads the plus operator to add two Money objects // and returns an anonymous object of type Money with the result of the addition friend const Money operator +(const Money& amount1, const Money& amount2); // overloads the minus operator to subtract two Money objects // and returns an anonymous object of type Money with the result of the subtraction friend const Money operator -(const Money& amount1, const Money& amount2); // overloads the equality operator to check if two money objects are exactly equal friend bool operator ==(const Money& amount1, const Money& amount2); // overloads the unary minus (negative) operator so that money amounts can be in negative friend const Money operator -(const Money& amount); // overloads the insertion operator to work with money types // cout<> (istream& inputStream, Money& amount); private: int dollars; //A negative amount is represented as negative dollars and int cents; //negative cents. Negative $4.50 is represented as -4 and -50 int dollarsPart(double amount) const; int centsPart(double amount) const; int round(double number) const; };

MainApp.cpp :

int main( ) { Money yourAmount, myAmount(10, 9); cout << "Enter an amount of money: "; yourAmount.input( ); cout << "Your amount is "; yourAmount.output( ); cout << endl; cout << "My amount is "; myAmount.output( ); cout << endl; if (yourAmount == myAmount) cout << "We have the same amounts. "; else cout << "One of us is richer. "; Money ourAmount = yourAmount + myAmount; yourAmount.output( ); cout << " + "; myAmount.output( ); cout << " equals "; ourAmount.output( ); cout << endl; Money diffAmount = yourAmount - myAmount; yourAmount.output( ); cout << " - "; myAmount.output( ); cout << " equals "; diffAmount.output( ); cout << endl; return 0; }

Money.cpp :

#include #include #include using namespace std; // the defination of the private function is given to you // define the rest of the class functions int Money::dollarsPart(double amount) const { return static_cast(amount); } int Money::centsPart(double amount) const { double doubleCents = amount*100; int intCents = (round(fabs(doubleCents)))%100;//% can misbehave on negatives if (amount < 0) intCents = -intCents; return intCents; } int Money::round(double number) const { return static_cast(floor(number + 0.5)); }

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 Databases Questions!