home *** CD-ROM | disk | FTP | other *** search
/ Mastering Microsoft Visual C++ 4 (2nd Edition) / VisualC4.ISO / cpp / ccurr.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-29  |  1.3 KB  |  54 lines

  1. // CCurr.h: CCurrency header file
  2.  
  3. #include <string.h>
  4. #include <iostream.h>
  5.  
  6. class CCurrency
  7. {
  8. private:
  9.    long Dollars;
  10.    int Cents; 
  11.  
  12. public:
  13.    CCurrency ()                       // default constructor
  14.       {
  15.       Dollars = Cents = 0;
  16.       }
  17.    CCurrency (long Dol, int Cen = 0)  // conversion constructor
  18.       {
  19.       SetAmount (Dol, Cen);
  20.       }  
  21.    CCurrency (double DolAndCen)       // conversion constructor
  22.       {
  23.       Dollars = long (DolAndCen);
  24.       Cents = int ((DolAndCen - Dollars) * 100.0 + 0.5);
  25.       }
  26.    void GetAmount (long *PDol, int *PCen)
  27.       {
  28.       *PDol = Dollars;
  29.       *PCen = Cents;
  30.       }               
  31.    void PrintAmount ()
  32.       {
  33.       cout.fill ('0');
  34.       cout.width (1);      
  35.       cout << '$' << Dollars << '.';
  36.       cout.width (2);
  37.       cout << Cents << '\n';
  38.       }
  39.    void SetAmount (long Dol, int Cen)
  40.       {
  41.       Dollars = Dol + Cen / 100;
  42.       Cents = Cen % 100;
  43.       }
  44.    friend CCurrency operator+ (const CCurrency &Curr1, 
  45.                                const CCurrency &Curr2);
  46. }; 
  47.    
  48. CCurrency operator+ (const CCurrency &Curr1, const CCurrency 
  49. &Curr2)
  50.    {
  51.    return CCurrency (Curr1.Dollars + Curr2.Dollars, 
  52.                      Curr1.Cents + Curr2.Cents);
  53.    }
  54.