(RationalNumber Class) Create a class RationalNumber (fractions) with the following capa- bilities:
a) Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions that are not in reduced form and avoids negative denominators.
b) Overload the addition, subtraction, multiplication and division operators for this class.
c) Overload the relational and equality operators for this class.
```
// RationalNumber class definition.
#ifndef RATIONAL_NUMBER_H
#define RATIONAL_NUMBER_H
class RationalNumber
{
public:
RationalNumber( int = 0, int = 1 ); // default constructor
RationalNumber operator+( const RationalNumber & ); // addition
RationalNumber operator-( const RationalNumber & ); // subtraction
RationalNumber operator*( const RationalNumber & ); // multiplication
RationalNumber operator/( const RationalNumber & ); // division
// relational operators
bool operator>( const RationalNumber & ) const;
bool operator<( const RationalNumber & ) const;
bool operator>=( const RationalNumber & ) const;
bool operator<=( const RationalNumber & ) const;
// equality operators
bool operator==( const RationalNumber & ) const;
bool operator!=( const RationalNumber & ) const;
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
#endif
```
```
// RationalNumber member-function definitions.
#include
#include
#include "RationalNumber.h"
using namespace std;
// RationalNumber constructor sets n and d and calls reduction
RationalNumber::RationalNumber( int n, int d )
{
numerator = n;
denominator = ( d > 0 ) ? d : 1; // validate denominator, 1 as default
reduction(); // invokes function reduction
} // end RationalNumber constructor
// overloaded + operator
RationalNumber RationalNumber::operator+( const RationalNumber &a )
{
return RationalNumber(
numerator * a.denominator + denominator * a.numerator,
denominator * a.denominator );
} // end function operator+
// overloaded - operator
RationalNumber RationalNumber::operator-( const RationalNumber &s )
{
return RationalNumber(
numerator * s.denominator - denominator * s.numerator,
denominator * s.denominator );
} // end function operator-
// overloaded * operator
RationalNumber RationalNumber::operator*( const RationalNumber &m )
{
return RationalNumber( numerator * m.numerator,
denominator * m.denominator );
} // end function operator*
// overloaded / operator
RationalNumber RationalNumber::operator/( const RationalNumber &d )
{
if ( d.numerator != 0 ) // check for a zero in numerator
{
return RationalNumber( numerator * d.denominator,
denominator * d.numerator );
} // end if
else
{
cout << "Divide by zero error: terminating program" << endl;
exit( 1 ); // stdlib function
} // end else
} // end function operator/
// overloaded < operator
bool RationalNumber::operator<( const RationalNumber &lr ) const
{
double thisVal = static_cast< double >( numerator ) / denominator;
58 double lrVal = static_cast< double >( lr.numerator ) / lr.denominator;
return thisVal < lrVal;
} // end function operator<
// overloaded > operator
bool RationalNumber::operator>( const RationalNumber &gr ) const
{
return gr < *this;
} // end function operator>
// overloaded <= operator
bool RationalNumber::operator<=( const RationalNumber &ler ) const
{
return !( *this > ler );
} // end function operator<=
// overloaded >= operator
bool RationalNumber::operator>=( const RationalNumber &ger ) const
{
return !( *this < ger );
} // end function operator>=
// overloaded == operator
bool RationalNumber::operator==( const RationalNumber &er ) const
{
return numerator == er.numerator && denominator == er.denominator;
} // end function operator==
// overloaded != operator
bool RationalNumber::operator!=( const RationalNumber &ner ) const
{
return !( *this == ner );
} // end function operator!=
// function printRational definition
void RationalNumber::printRational() const
{
if ( numerator == 0 ) // print fraction as zero
cout << numerator;
else if ( denominator == 1 ) // print fraction as integer
cout << numerator;
else
cout << numerator << '/' << denominator;
} // end function printRational
// function reduction definition
void RationalNumber::reduction()
{
int largest, gcd = 1; // greatest common divisor;
largest = ( numerator > denominator ) ? numerator : denominator;
for ( int loop = 2; loop <= largest; loop++ )
if ( numerator % loop == 0 && denominator % loop == 0 )
gcd = loop;
numerator /= gcd;
denominator /= gcd;
} // end function reduction
```
```
// RationalNumber test program.
#include
#include "RationalNumber.h"
using namespace std;
int main()
{
RationalNumber c( 7, 3 ), d( 3, 9 ), x;
c.printRational();
cout << " + " ;
d.printRational();
cout << " = ";
x = c + d; // test overloaded operators + and =
x.printRational();
cout << '\n';
c.printRational();
cout << " - " ;
d.printRational();
cout << " = ";
x = c - d; // test overloaded operators - and =
x.printRational();
cout << '\n';
c.printRational();
cout << " * " ;
d.printRational();
cout << " = ";
x = c * d; // test overloaded operators * and =
x.printRational();
cout << '\n';
c.printRational();
cout << " / " ;
d.printRational();
cout << " = ";
x = c / d; // test overloaded operators / and =
x.printRational();
cout << '\n';
c.printRational();
cout << " is:\n";
// test overloaded greater than operator
cout << ( ( c > d ) ? " > " : " <= " );
d.printRational();
cout << " according to the overloaded > operator\n";
// test overloaded less than operator
cout << ( ( c < d ) ? " < " : " >= " );
d.printRational();
cout << " according to the overloaded < operator\n";
// test overloaded greater than or equal to operator
cout << ( ( c >= d ) ? " >= " : " < " );
d.printRational();
cout << " according to the overloaded >= operator\n";
// test overloaded less than or equal to operator
cout << ( ( c <= d ) ? " <= " : " > " );
d.printRational();
cout << " according to the overloaded <= operator\n";
// test overloaded equality operator
cout << ( ( c == d ) ? " == " : " != " );
d.printRational();
cout << " according to the overloaded == operator\n";
// test overloaded inequality operator
cout << ( ( c != d ) ? " != " : " == " );
d.printRational();
cout << " according to the overloaded != operator" << endl;
} // end main
```
7/3 + 1/3 = 8/3
7/3 - 1/3 = 2
7/3 * 1/3 = 7/9
7/3 / 1/3 = 7
7/3 is:
> 1/3 according to the overloaded > operator
>= 1/3 according to the overloaded < operator
>= 1/3 according to the overloaded >= operator
> 1/3 according to the overloaded <= operator
!= 1/3 according to the overloaded == operator
!= 1/3 according to the overloaded != operator
You might also like to view...
Typing text by using a number sequence to represent a character is an example of a(n) ________
A) private key B) code group C) public key D) cipher text
The __________, which is part of the developer tools in a modern web browser, can be used to enter test code and view error message related to JavaScript.
A. address bar B. bookmarks list C. console D. emulator