//DISPLAY 11.3 Money ClassVersion 1 //Program to demonstrate the class Money. #include #include #include using namespace std; //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 amount1 and amount2. friend bool 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 must 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 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 -$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; }; int digit_to_int(char c); //Function declaration for function used in the definition of Money::input: //Precondition: c is one of the digits '0' through '9'. //Returns the integer for the digit; for example, 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; } bool equal(Money amount1, Money amount2) { return (amount1.all_cents == amount2.all_cents); } Money::Money(long dollars, int cents) { if(dollars*cents < 0) //If one is negative and one is positive { cout << "Illegal values for dollars and cents.\n"; exit(1); } all_cents = dollars*100 + cents; } Money::Money(long dollars) : all_cents(dollars*100) { //Body intentionally blank. } Money::Money( ) : all_cents(0) { //Body intentionally blank. } double Money::get_value( ) { return (all_cents * 0.01); } //Uses iostream, cctype, cstdlib: void Money::input(istream& ins) { char one_char, decimal_point, digit1, digit2; //digits for the amount of cents long dollars; int cents; bool 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 cstdlib and iostream: 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 (static_cast(c) - static_cast('0') ); }