#pragma once #include #include class Rational; /////////////////////////// // Function declarations // /////////////////////////// // Calculate n + p. Rational operator+( int const n, Rational const &p ); // Calculate n - p. Rational operator-( int const n, Rational const &p ); // Calculate n*p. Rational operator*( int const n, Rational const &p ); // Calculate n/p. Rational operator/( int const n, Rational const &p ); // Determine whether n == p. bool operator==( int const n, Rational const &p ); // Determine whether n != p. bool operator!=( int const n, Rational const &p ); // Determine whether n < p. bool operator<( int const n, Rational const &p ); // Determine whether n <= p. bool operator<=( int const n, Rational const &p ); // Determine whether n >= p. bool operator>=( int const n, Rational const &p ); // Determine whether n > p. bool operator>( int const n, Rational const &p ); // Insert p into an output stream. std::ostream &operator<<( std::ostream &out, Rational const &p ); ////////////////////// // Class definition // /////////////////////// class Rational { public: // NEW: Construct a rational number and put it into // normal form. Rational( int const numerator = 0, int const denominator = 1 ); // NEW: Return the numerator. int numer() const; // NEW: Return the denominator. int denom() const; // Calculate this + q. Rational operator+( Rational const &q ) const; // Calculate this + n. Rational operator+( int const n ) const; // Calculate this - q. Rational operator-( Rational const &q ) const; // Calculate this - n. Rational operator-( int const n ) const; // Calculate this*q. Rational operator*( Rational const &q ) const; // Calculate this*n. Rational operator*( int const n ) const; // Calculate this/q. Rational operator/( Rational const &q ) const; // Calculate this/n. Rational operator/( int const n ) const; // Return this rational number unchanged. Rational operator+() const; // Return the additive inverse of this rational number. Rational operator-() const; // Determine whether this == q. bool operator==( Rational const &q ) const; // Determine whether this == n. bool operator==( int const n ) const; // Determine whether this != q. bool operator!=( Rational const &q ) const; // Determine whether this != n. bool operator!=( int const n ) const; // Determine whether this < q. bool operator<( Rational const &q ) const; // Determine whether this < n. bool operator<( int const n ) const; // Determine whether this <= q. bool operator<=( Rational const &q ) const; // Determine whether this <= n. bool operator<=( int const n ) const; // Determine whether this >= q. bool operator>=( Rational const &q ) const; // Determine whether this >= n. bool operator>=( int const n ) const; // Determine whether this > q. bool operator>( Rational const &q ) const; // Determine whether this > n. bool operator>( int const n ) const; // Return the multiplicative inverse. Rational inverse() const; // Return the absolute value. Rational abs() const; // Return this rational number as a double. double real() const; // Return a string representation. std::string to_string() const; private: // NEW: The numerator and denominator are private so // users cannot place the object into an invalid or // non-normal form. // - Because the user now uses numer() and denom(), // these can be given clearer identifiers. int numerator_; int denominator_; // NEW: Calculate the greatest common divisor of m and n. // // This is static because calculating a GCD does not // require a particular Rational object. static int gcd( int m, int n ); };