// rational.h -- interface to simple Rational number class. // cse143 assignment 1 sample solution, Jan. 1999. HP #include class Rational { public: // constructors Rational(); // = 0/1 Rational(int n); // = n/1 Rational(int n, int d); // = n/d // component access friend int num(Rational); friend int denom(Rational); // arithmetic operations friend Rational operator+(Rational, Rational); friend Rational operator-(Rational); friend Rational operator-(Rational, Rational); friend Rational operator*(Rational, Rational); friend Rational operator/(Rational, Rational); // relational operators friend bool operator< (Rational, Rational); friend bool operator<=(Rational, Rational); friend bool operator==(Rational, Rational); friend bool operator!=(Rational, Rational); friend bool operator> (Rational, Rational); friend bool operator>=(Rational, Rational); // stream output: r displayed as numerator/denominator friend ostream& operator<<(ostream &s, Rational r); private: int nm, dn; // Numerator and denominator of Rational nm/dn. // Invariant: dn>0 and nm and dn have no common factors. // other than 1. 0 is uniquely represented as 0/1. };