home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch12 / fraction.h < prev    next >
C/C++ Source or Header  |  2005-06-16  |  2KB  |  54 lines

  1. //              fraction.h
  2. //
  3. // Contains the declaration of the Fraction class
  4.  
  5. // Include Files
  6. #include <iostream.h>                                
  7.  
  8. class Fraction {
  9.                                                      // Note 1
  10.     friend ostream& operator << ( ostream& str, Fraction f );
  11.     // PRECONDITION:  str must be available for output.
  12.     //
  13.     // POSTCONDITION: f will be written to str.
  14.  
  15. public:
  16.     Fraction() {  num = 0; denom = 1; }
  17.     // PRECONDITION:  none.
  18.     //
  19.     // POSTCONDITION: the object is initialized as 0/1.
  20.  
  21.     Fraction ( int n, int d );
  22.     // PRECONDITION:  none.
  23.     //
  24.     // POSTCONDITION: If d is not equal to 0, the object is 
  25.     //                initialized as n/d. Otherwise it is 
  26.     //                initialized as 0/1.
  27.  
  28.     int numerator() const { return num; }
  29.     // PRECONDITION:  none.
  30.     //
  31.     // POSTCONDITION: the value of num is returned.
  32.  
  33.     int denominator() const { return denom; }
  34.     // PRECONDITION:  none.
  35.     //
  36.     // POSTCONDITION: the value of denom is returned.
  37.  
  38.     Fraction operator -() const;                     // Note 2
  39.     // PRECONDITION:  none.
  40.     //
  41.     // POSTCONDITION: the return value is the negative 
  42.     //                of the invoking object.
  43.  
  44.     Fraction operator +( Fraction f ) const;         // Note 3
  45.     // PRECONDITION:  none.
  46.     //
  47.     // POSTCONDITION: The return value is the fraction sum of
  48.     //                the invoking object and f.
  49.  
  50. private:
  51.     int num;
  52.     int denom;
  53. };
  54.