// File: RationalOverload.h // Simple Operator Overload Example // A rational number is also known as a fraction. // Its data members are a numerator and denominator. // It carries out ordinary arithmentic operations, // The assignment operators update the attributes // of the object // The arithmetic operators return a new object, and, while they can be // members, are implemented as associated operators. // You may assume: // -- if the rational number is negative, the numerator will be negative // -- all stored rationals are reduced to lowest terms #ifndef RATIONAL_H #define RATIONAL_H #include using namespace std; class Rational { public: // default constructor: parameters are numerator and denominator resp. Rational(int n = 0, int d = 1 ); void setNumerator(int n); void setDenominator(int d); int getNumerator() const; int getDenominator() const; // assignment operators Rational& plusEquals(const Rational &right); // Rational& minusEquals(const Rational &right); // Rational& starEquals(const Rational &right); // Rational& slashEquals(const Rational &right); // division by zero terminates void reduce(); // reduce fraction to lowest terms private: int numerator; // numerator of fraction int denominator; // denominator of fraction }; // The arithmetic operators are overloaded as associates. // They could syntactically be members, but since they really aren't // mutators, facilitators, or inspectors, it's better they be here. Rational plusOp(const Rational &left,const Rational &right); Rational minusOp(const Rational &left,const Rational &right); Rational starOp(const Rational &left,const Rational &right); Rational slashOp(const Rational &left,const Rational &right); // division by zero terminates #endif