I am writing a rational number class and need to know if this is the correct way to do it. Can someone please let me know if I'm on track or not? Thanks.
class RationalNumber
{
public:
RationalNumber( int = 0, int = 1); // constructor
RationalNumber operator+( const RationalNumber& ) const; // addition
RationalNumber operator-( const RationalNumber& ) const; // subtraction
RationalNumber operator*( const RationalNumber& ) const; // multiplication
RationalNumber operator/( const RationalNumber& ) const; // division
// relational operators
bool operator>( const RationalNumber& ) const; // greater than
bool operator<( const RationalNumber& ) const; // less than
bool operator>=( const RationalNumber&, ) const; // greater than or equal to
bool operator<=( const RationalNumber&, ) const; // less than or equal to
// equality operators
bool operator==( const RationalNumber & ) const; // equality operator
// inequality operator
bool operator!=( const RationalNumber &right ) const
{
return ! ( *this == right );
} // end function !=
void printRational() const; // display rational number
private:
int numerator; // private variable numerator
int denominator; // private variable denominator
void reduction(); // function for fraction reduction
}; // end class RationalNumber
RationalNumber::RationalNumber( int n, int d )
{
denominator = ( d > 0 ? d : 1 ); validate that denominator to insure it is positive
numerator = n;
reduction();
}
RationalNumber RationalNumber::operator+( const RationalNumber &x ) const
{
return RationalNumber( numerator * x.denominator + denominator * x.denominator,
denominator * x.denominator );
}