Source of fraction.h


  1: // Filename: FRACTION.H
  2: // Purpose:  Specification file for class Fraction.

  4: // Note that in the following pre/post-conditions, "self" simply refers
  5: // to the fractional object to which the function in question is being
  6: // applied (as opposed to the "other" fraction which is passed in as
  7: // a parameter).  Also, "in reduced form" means "in lowest terms", i.e.,
  8: // the numerator and denominator of the fraction have no common factor.

 10: #ifndef FRACTION_H
 11: #define FRACTION_H

 13: class Fraction
 14: {
 15:     friend istream& operator>>(/* in */  istream& inStream,
 16:                                /* out */ Fraction& f);
 17:     // Pre:  The next item in the standard input stream must have the
 18:     //       form: int/int (with spaces possibly occuring before and/or
 19:     //       after the '/'.  The second int value must be non-zero.
 20:     // Post: The value from the standard input stream having the form
 21:     //       int/int has been read into self as numerator/denominator.
 22:     //       That is "numerator" has been initialized with the int
 23:     //       value preceding the '/' and "denominator" has been
 24:     //       initialized with the int value following the '/'.

 26:     friend ostream& operator<<(/* in */ ostream& outStream,
 27:                                /* in */ const Fraction& f);
 28:     // Pre:  self has been initialized.
 29:     // Post: self is printed in the form numerator/denominator,
 30:     //       unless the fraction is actually an integer, in which
 31:     //       case it is simply printed as that integer.  Also, any
 32:     //       negative fraction is printed with a negative numerator
 33:     //       and a positive denominator.  All fractions are displayed
 34:     //       in reduced form (lowest terms).


 37: public:

 39:     Fraction(/* in */ int numer = 0, /* in */ int denom = 1);
 40:     // Pre:  none
 41:     // Post: "numerator" is set to "numer", "denominator" to "denom",
 42:     //       if both parameters have been supplied (unless denom == 0,
 43:     //       in which case the following message is displayed and the
 44:     //       program is terminated):
 45:     //       Error: Zero denominator not permitted.
 46:     //              Program is now terminating
 47:     //              directly from class constructor.
 48:     //       If only one parameter is supplied, "numerator" is set to
 49:     //       that value and denominator defaults to a value of 1.
 50:     //       If both parameters are omitted, then "numerator" defaults
 51:     //       to a value of 0 and "denominator" to a value of 1.

 53:     void Set(/* in */ int numer = 0, /* in */ int denom = 1);
 54:     // Pre:  none
 55:     // Post: "numerator" is set to "numer", "denominator" to "denom",
 56:     //       if both parameters have been supplied (unless denom == 0,
 57:     //       in which case the following message is displayed and the
 58:     //       program is terminated):
 59:     //       Error: Zero denominator not permitted.
 60:     //              Program is now terminating
 61:     //              directly from function Set.
 62:     //       If only one parameter is supplied, "numerator" is set to
 63:     //       that value and denominator defaults to a value of 1.
 64:     //       If both parameters are omitted, then "numerator" defaults
 65:     //       to a value of 0 and "denominator" to a value of 1.

 67:     Fraction operator+(/* in */ const Fraction& otherFrac) const;
 68:     // Pre:  Both self and "otherFrac" have been initialized.
 69:     // Post: Value of self + otherFrac is returned, in reduced form.

 71:     Fraction operator-(/* in */ const Fraction& otherFrac) const;
 72:     // Pre:  Both self and "otherFrac" have been initialized.
 73:     // Post: Value of self - otherFrac is returned, in reduced form.

 75:     Fraction operator*(/* in */ const Fraction& otherFrac) const;
 76:     // Pre:  Both self and "otherFrac" have been initialized.
 77:     // Post: Value of self * otherFrac is returned, in reduced form.

 79:     Fraction operator/(/* in */ const Fraction& otherFrac) const;
 80:     // Pre:  Both self and "otherFrac" have been initialized.
 81:     // Post: Value of self / otherFrac is returned, in reduced form.


 84:     bool operator<(/* in */ const Fraction& otherFrac) const;
 85:     // Pre:  Both self and "otherFrac" have been initialized.
 86:     // Post: Returned value is "true" if self is less than otherFrac
 87:     //       and "false" otherwise.

 89:     bool operator==(/* in */ const Fraction& otherFrac) const;
 90:     // Pre:  Both self and "otherFrac" have been initialized.
 91:     // Post: Returned value is "true" if self and otherFrac have the
 92:     //       same value, and "false" otherwise.

 94: private:
 95:     int  numerator;
 96:     int  denominator;
 97: };

 99: #endif