//Program to demonstrate the class Money in Display 8.6. #include #include #include const int FALSE = 0; const int TRUE = 1; //Class for amounts of money in U.S. currency. class Money { public: friend Money operator +(const Money& amount1, const Money& amount2); friend Money operator -(const Money& amount1, const Money& amount2); //Precondition: amount1 and amount2 have been given values. //Returns amount 1 minus amount2. friend Money operator -(const Money& amount); //Precondition: amount has been given a value. //Returns the negative of the value of amount. friend int operator ==(const Money& amount1, const Money& amount2); Money(long dollars, int cents); Money(long dollars); Money( ); double get_value( ) const; void input(istream& ins); void output(ostream& outs) const; private: long all_cents; }; //Prototype for use in the definition of Money::input: int digit_to_int(char c); //Precondition: c is one of the digits '0' through '9'. //Returns the integer for the digit; e.g., digit_to_int('3') returns 3. //If your implementation does not include labs, then removed the //comments delimiters around the following prototype and around the //corresponding function definition, which is at the end of this file. /* long labs(long number); //returns the absolute value of number. */ int main( ) { Money cost(1, 50), discount(0, 15), total, amount; total = cost - discount; cout << "cost = "; cost.output(cout); cout << endl; cout << "discount = "; discount.output(cout); cout << endl; cout << "total bill = "; total.output(cout); cout << endl; cout << "The discount changes the price by "; amount = -discount; amount.output(cout); cout << endl; return 0; } Money operator -(const Money& amount1, const Money& amount2) { Money temp; temp.all_cents = amount1.all_cents - amount2.all_cents; return temp; } Money operator -(const Money& amount) { Money temp; temp.all_cents = -amount.all_cents; return temp; } Money operator +(const Money& amount1, const Money& amount2) { Money temp; temp.all_cents = amount1.all_cents + amount2.all_cents; return temp; } int operator ==(const Money& amount1, const Money& amount2) { return (amount1.all_cents == amount2.all_cents); } Money::Money(long dollars, int cents) { all_cents = dollars*100 + cents; } Money::Money(long dollars) { all_cents = dollars*100; } Money::Money( ) { all_cents = 0; } double Money::get_value( ) const { return (all_cents * 0.01); } //Uses iostream.h, ctype.h, stdlib.h, and constants TRUE and FALSE: void Money::input(istream& ins) { char one_char, decimal_point, digit1, digit2; //digits for the amount of cents long dollars; int cents, negative;//set to TRUE if input is negative. ins >> one_char; if (one_char == '-') { negative = TRUE; ins >> one_char; //read '$' } else negative = FALSE; //if input is legal, then one_char == '$' ins >> dollars >> decimal_point >> digit1 >> digit2; if ( one_char != '$' || decimal_point != '.' || !isdigit(digit1) || !isdigit(digit2) ) { cout << "Error illegal form for money input\n"; exit(1); } cents = digit_to_int(digit1)*10 + digit_to_int(digit2); all_cents = dollars*100 + cents; if (negative) all_cents = -all_cents; } //Uses stdlib.h and iostream.h: void Money::output(ostream& outs) const { long positive_cents, dollars, cents; positive_cents = labs(all_cents); dollars = positive_cents/100; cents = positive_cents%100; if (all_cents < 0) outs << "-$" << dollars << '.'; else outs << "$" << dollars << '.'; if (cents < 10) outs << '0'; outs << cents; } int digit_to_int(char c) { return ( int(c) - int('0') ); } /* long labs(long number) { if (number < 0) return (-number); else return number; } */