// Inserter - provide an inserter for USDollar #include #include #include // USDollar - represent the greenback class USDollar { friend ostream& operator<<(ostream& out, USDollar& d); public: // construct a dollar object with an initial // dollar and cent value USDollar(int d = 0, int c = 0) { nDollars = d; nCents = c; } // rationalize - normalize the number of nCents by // adding a dollar for every 100 nCents void rationalize() { nDollars += (nCents / 100); nCents %= 100; } protected: int nDollars; int nCents; }; // inserter - output a string description // (this version handles the case of cents // less than 10) ostream& operator<<(ostream& out, USDollar& d) { char old = out.fill(); out << "$" << d.nDollars << "." << setfill('0') << setw(2) << d.nCents; // replace the old fill character out.fill(old); return out; } int main(int nArgc, char* pszArgs[]) { USDollar d1(1, 60); cout << "Dollar d1 = " << d1 << "\n"; USDollar d2(1, 5); cout << "Dollar d2 = " << d2 << "\n"; return 0; }