// // Class name: rationals_v1 // Author: B. Massingill // // requires #include // -------- definition of class for rational numbers -------- // // an object of this class represents a rational number, i.e., a // fraction // class RationalNum { private: // ---- member variables ---- int numerator; int denominator; public: // ---- constructors ---- RationalNum(const int n=0, const int d=1); // ---- member functions ---- // read from input stream // precondition: iStr is an input stream (already successfully // opened if an ifstream) // postcondition: // if iStr contains a fraction in the form integer/integer, // that fraction is read in and return value is true // otherwise return value is false bool stream_in(istream& iStr); // print to output stream // precondition: oStr is an output stream (already successfully // opened if an ofstream) // postcondition: fraction has been printed on oStr, in the form // numerator/denominator void stream_out(ostream& oStr); // "reduce" to simplest terms // precondition: none // postcondition: denominator is non-negative, numerator and // denominator have no common factors void reduce(void); // ---- friend functions ---- // comparison functions // precondition: none // postcondition: return value indicates whether p == q // (for equals), p < q (for lessThan), or p > q (for // greaterThan) friend bool equals(const RationalNum p, const RationalNum q); friend bool lessThan(const RationalNum p, const RationalNum q); friend bool greaterThan(const RationalNum p, const RationalNum q); // arithmetic functions // precondition: none // postcondition: return value is p+q, p-q, p*q, p/q as // indicated by function name friend RationalNum add(const RationalNum p, const RationalNum q); friend RationalNum subtract(const RationalNum p, const RationalNum q); friend RationalNum multiply(const RationalNum p, const RationalNum q); friend RationalNum divide(const RationalNum p, const RationalNum q); } ;