//Program to demonstrate the class Money. #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 add(Money amount1, Money amount2); //Precondition: amount1 and amount2 have been given values. //Returns the sum of the values of amount 1 and amount2. friend int equal(Money amount1, Money amount2); //Precondition: amount1 and amount2 have been given values. //Returns true if the amount1 and amount2 have the same value; //otherwise, returns 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 should be negative. Money(long dollars); //Initializes the object so its value represents $dollars.00. Money( ); //Initializes the object so its value represents $0.00. double get_value( ); //Precondition: The calling object has been given a value. //Returns the amount of money recorded in the data portion //of the calling object. 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 as in -$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); //Precondition: If outs is a file output stream, then outs has //already been connected 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; }; //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 your_amount, my_amount(10, 9), our_amount; cout << "Enter an amount of money: "; your_amount.input(cin); cout << "Your amount is "; your_amount.output(cout); cout << endl; cout << "My amount is "; my_amount.output(cout); cout << endl; if (equal(your_amount, my_amount)) cout << "We have the same amounts.\n"; else cout << "One of us is richer.\n"; our_amount = add(your_amount, my_amount); your_amount.output(cout); cout << " + "; my_amount.output(cout); cout << " equals "; our_amount.output(cout); cout << endl; return 0; } Money add(Money amount1, Money amount2) { Money temp; temp.all_cents = amount1.all_cents + amount2.all_cents; return temp; } int equal(Money amount1, 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( ) { 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) { 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') ); }