// USDollarMemberAdd - define the addition and increment // operators as members #include #include // USDollar - represent the greenback class USDollar { public: // construct a dollar object with an initial // dollar and cent value USDollar(int d = 0, int c = 0) { // store of the initial values locally dollars = d; cents = c; rationalize(); } // rationalize - normalize the number of cents by // adding a dollar for every 100 cents void rationalize() { dollars += (cents / 100); cents %= 100; } // output- display the value of this object // to the standard output object void output() { cout << "$" << dollars << "." << cents; } //operator+ - add the current object to s2 and // return in a new object USDollar operator+(USDollar& s2) { int cents = this->cents + s2.cents; int dollars = this->dollars + s2.dollars; return USDollar(dollars, cents); } //operator++ - increment the current object USDollar& operator++() { cents++; rationalize(); return *this; } protected: int dollars; int cents; }; int main(int nArgc, char* pszArgs[]) { USDollar d1(1, 60); USDollar d2(2, 50); USDollar d3(0, 0); // first demonstrate a binary operator d3 = d1 + d2; d1.output(); cout << " + "; d2.output(); cout << " = "; d3.output(); cout << "\n"; // now check out a unary operator ++d3; cout << "After incrementing it equals "; d3.output(); cout << "\n"; return 0; }