//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 operator *(const Money& amount1, int factor); friend Money operator +(const Money& amount1, const Money& amount2); //Precondition: amount1 and amount2 have been given values. //Returns the sum of the values of amount1 and amount2. friend bool 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); //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( ) const; //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; }; bool operator> (const Money& amount1, const Money& amount2); 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; //Now I double my_amount my_amount = my_amount*2; cout << "Now My amount is "; my_amount.output(cout); cout << endl; if (your_amount > my_amount) cout << "You are richer .\n"; else cout << ".... .\n"; our_amount = your_amount + my_amount; your_amount.output(cout); cout << " + "; my_amount.output(cout); cout << " equals "; our_amount.output(cout); cout << endl; //Suppose now I received a gift of $10 cout <<" Suppose now I received a gift of $10...\n"; my_amount = my_amount+10; cout <<"After that: I own "; my_amount.output(cout); //Two different initialization Money balance=Money(100); Money balance2 (100); Money b3=200; cout <(c) - static_cast('0') ); // old C-style: return ((int)c -(int)('0') ); } //Any extra function declarations from Display 11.3 go here. 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, int factor) { Money temp; temp.all_cents = amount.all_cents * factor; return temp; } bool operator ==(const Money& amount1, const Money& amount2) { return (amount1.all_cents == amount2.all_cents); } //non-friend function bool operator> (const Money& amount1, const Money& amount2) { if (amount1.get_value()>amount2.get_value()) return true; else return false; }