Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement the class defined in header file. You also need to implement a test program to test the validity of your implementation. Your test program

Implement the class defined in header file. You also need to implement a test program to test the validity of your implementation. 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

25 Vba Macros For Data Analysis In Microsoft Excel

Authors: Klemens Nguyen

1st Edition

B0CNSXYMTC, 979-8868455629

More Books

Students also viewed these Databases questions