#include #include #include using namespace std; //Class for amounts of money in U.S. currency. class Money { public: Money( ); Money(double amount); Money(int theDollars, int theCents); Money(int theDollars); double GetAmount( ) const; int GetDollars( ) const; int GetCents( ) const; void Input( ); //Reads the dollar sign as well as the amount number. void Output( ) const; private: //A negative amount is represented as negative dollars and //negative cents. Negative $4.50 is represented as -4 and -50 int dollars; int cents; int dollarsPart(double amount) const; int centsPart(double amount) const; int round(double number) const; }; const Money operator+ (const Money& amount1, const Money& amount2); const Money operator- (const Money& amount1, const Money& amount2); bool operator== (const Money& amount1, const Money& amount2); const Money operator- (const Money& amount); 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.\n"; else cout << "One of us is richer.\n"; 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; // dlf code ourAmount = 10 + ourAmount; cout << endl << "ourAmount: "; ourAmount.Output ( ) ; cout << endl; return 0; } const Money operator+ (const Money& amount1, const Money& amount2) { int allCents1 = amount1.GetCents( ) + amount1.GetDollars( )*100; int allCents2 = amount2.GetCents( ) + amount2.GetDollars( )*100; int sumAllCents = allCents1 + allCents2; int absAllCents = abs(sumAllCents); //Money can be negative. int finalDollars = absAllCents/100; int finalCents = absAllCents%100; if (sumAllCents < 0) { finalDollars = -finalDollars; finalCents = -finalCents; } return Money(finalDollars, finalCents); } //Uses cstdlib: const Money operator- (const Money& amount1, const Money& amount2) { int allCents1 = amount1.GetCents( ) + amount1.GetDollars( )*100; int allCents2 = amount2.GetCents( ) + amount2.GetDollars( )*100; int diffAllCents = allCents1 - allCents2; int absAllCents = abs(diffAllCents); int finalDollars = absAllCents/100; int finalCents = absAllCents%100; if (diffAllCents < 0) { finalDollars = -finalDollars; finalCents = -finalCents; } return Money(finalDollars, finalCents); } bool operator== (const Money& amount1, const Money& amount2) { return ((amount1.GetDollars( ) == amount2.GetDollars( )) && (amount1.GetCents( ) == amount2.GetCents( ))); } const Money operator- (const Money& amount) { return Money( -amount.GetDollars( ), -amount.GetCents( )); } Money::Money( ): dollars(0), cents(0) {/*Body intentionally empty.*/} Money::Money(double amount) : dollars(dollarsPart(amount)), cents(centsPart(amount)) {/*Body intentionally empty*/} Money::Money(int theDollars) : dollars(theDollars), cents(0) {/*Body intentionally empty*/} //Uses cstdlib: Money::Money(int theDollars, int theCents) { if ((theDollars < 0 && theCents > 0) || (theDollars > 0 && theCents < 0)) { cout << "Inconsistent money data.\n"; exit(1); } dollars = theDollars; cents = theCents; } double Money::GetAmount( ) const { return (dollars + cents*0.01); } int Money::GetDollars( ) const { return dollars; } int Money::GetCents( ) const { return cents; } //Uses iostream and cstdlib: void Money::Output( ) const { int absDollars = abs(dollars); int absCents = abs(cents); if (dollars < 0 || cents < 0)//accounts for dollars == 0 or cents == 0 cout << "$-"; else cout << '$'; cout << absDollars; if (absCents >= 10) cout << '.' << absCents; else cout << '.' << '0' << absCents; } //Uses iostream and cstdlib: void Money::Input( ) { char dollarSign; cin >> dollarSign; //hopefully if (dollarSign != '$') { cout << "No dollar sign in Money Input.\n"; exit(1); } double amountAsDouble; cin >> amountAsDouble; dollars = dollarsPart(amountAsDouble); cents = centsPart(amountAsDouble); } 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)); }