Question
Implement the class defined in header file. Also need to implement a test.cpp program to test the validity of your implementation.cpp. Your test program must
Implement the class defined in header file. Also need to implement a test.cpp program to test the validity of your implementation.cpp. Your test program must use all overloaded operators and implemented functions.
# ifndef RATIONAL_H
# define RATIONAL_H
// forward references
class ostream;
class istream;
//
// class rational
// rational number data abstraction
//
class rational {
public:
// constructors
rational ();
rational (int);
rational (int, int);
rational (const rational &);
// accessor functions
int numerator () const;
int denominator () const;
// assignments
void operator = (const rational &);
void operator += (const rational &);
void operator -= (const rational &);
void operator *= (const rational &);
void operator /= (const rational &);
// comparison
int compare (const rational &) const;
// conversions
operator float () const;
private:
// data areas
int top;
int bottom;
// operations used internally
void normalize ();
};
//
// function prototypes
//
rational operator + (const rational &, const rational &);
rational operator - (const rational &, const rational &);
rational operator * (const rational &, const rational &);
rational operator / (const rational &, const rational &);
rational operator - (const rational &);
// comparison operations
int operator == (const rational &, const rational &);
int operator != (const rational &, const rational &);
int operator < (const rational &, const rational &);
int operator <= (const rational &, const rational &);
int operator > (const rational &, const rational &);
int operator >= (const rational &, const rational &);
// input and output functions
ostream & operator << (ostream &, const rational &);
istream & operator >> (istream &, rational &);
int floor(const rational &);
unsigned int gcd(unsigned int, unsigned int);
//
// inline functions
//
inline rational::rational() : top(0), bottom(1)
{
// no further initialization required
}
inline rational::rational(int numerator) : top(numerator), bottom(1)
{
// no further initialization required
}
inline rational::rational(const rational & value)
: top(value.top), bottom(value.bottom)
{
// no further initialization required
}
inline int rational::numerator() const
{
// return numerator field of rational number
return top;
}
inline int rational::denominator() const
{
// return denominator field of rational number
return bottom;
}
#endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started