//Program to demonstrate the class Money. (This is an improved //version of the class Money which we gave in Display 8.3.) #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); //Precondition: amount1 and amount2 have been given values. //Returns the sum of the values of amount 1 and amount2. friend int 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, returns false. 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. 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.\n"; else cout << "Things seem normal.\n"; return 0; } 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') ); }