home *** CD-ROM | disk | FTP | other *** search
- // Chap23_1.cpp
- #include <iostream.h>
- #include <iomanip.h>
- class USDollar
- {
- public:
- USDollar(double v = 0.0)
- {
- dollars = v;
- cents = int((v - dollars) * 100.0 + 0.5);
- }
- operator double()
- {
- return dollars + cents / 100.0;
- }
- void display(ostream& out)
- {
- out << '$' << dollars << '.'
- //set fill to 0Æs for cents
- << setfill('0') << setw(2) << cents
- //now put it back to spaces
- << setfill(' ');
- }
-
- protected:
- unsigned int dollars;
- unsigned int cents;
- };
-
- //operator<< - overload the inserter for our class
- ostream& operator<< (ostream& o, USDollar& d)
- {
- d.display(o);
- return o;
- }
-
- int main()
- {
- USDollar usd(1.50);
- cout << "Initially usd = " << usd << "\n";
- usd = 2.0 * usd;
- cout << "then usd = " << usd << "\n";
- return 0;
- }
-