(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...
Tape and CDs are example of storage devices.
Answer the following statement true (T) or false (F)
How many files are displayed now, more than before? Explain.
Work with hidden files in Linux. a. In Linux, files with names that begin with a β.β (single dot) are not shown by default. While dot-files have nothing else special about them, they are called hidden files because of this feature. Examples of hidden files are .file5, .file6, .file7. b. Use ls -l to display the files stored in the analyst home directory.
[analyst@secOps ~]$ ls βlc. Use the ls -la command to display all files in the home directory of analyst, including the hidden files.
[analyst@secOps ~]$ ls βla
Which of the following is a valid example of a file identifier in the header of a file?
a. ~~~*PH b. START __ -- __ -- PNG c. %PNG d. None of the above
The ____ is the range of colors allowed by the CMYK mode.
a. spectrum b. palette c. gamut d. gauntlet