# OOP  - Assignment 4

Topic:             Overloaded Operators and Rational Numbers

## Create a class to represent rational numbers with the following functionality:

Constructor(s):

The user of the class should be able to create an instance of a rational number passing in 0-2 arguments. If no arguments are passed in the value should be 0, for example 0/1.  If one argument is passed in, it should be considered a whole number and thus be represented over one in radical form, for example 5/1.  If two numbers are passed in, the first should be considered the numerator the second the denominator.  Be sure to reduce the fraction completely.

Unary Operators:

++   post and pre increment operators.  These operators should add one to the number.  Be sure to reduce the resulting rational.

--     post and pre decrement operators.  These operators should subtract one from number.  Be sure to reduce the resulting rational.

+=, -=, *=, \= operators.  These operators should modify the numerator appropriately according to the rValue passed in.

()     function operator.  Overload this operator to return a string in the form “n/d”.

double()   type cast operator.  Overload this operator to return a double for the numerator divided by the denominator.

Binary Operators:

All of the binary operators listed in the class definition should be overloaded.  Each of these operators should work with whole numbers and other rational numbers.  Be sure to reduce the resulting radical.

Non-member Binary Operators:

The stream insertion << and stream extraction >> operators should get and show the value of the radical.

The stream-extraction operator is only required to get the rational from the user in this form n/d

The stream insertion operator should show the radical with a forward slash ‘/’ appearing between the numerator and denominator with one exception.  If the denominator is 1, only the numerator should be displayed.

+, -, *, and / should be overloaded with lValues of type long. This will allow operations like 1 + r where r is an object of type rational.

Private Function(s):

A private reduce fraction and common denominator functions will be useful.

Watch out for negative values in both the numerator and denominator.

 Criteria Points Implement ~12 missing functions 36 Create a test program that tests all operators and methods of the rational class. 14

//Rational.h

#include<iostream>

#include<string>

using std::ostream;

using std::istream;

using std::string;

#ifndef RATIONAL_H

#define RATIONAL_H

class Rational

{

public:

Rational(long NewNumerator = 0, long NewDenominator = 1);

const Rational & operator=(const Rational & rValue);

bool operator==(const Rational& rValue) const;

bool operator>(const Rational& rValue) const;

bool operator<(const Rational& rValue) const;

bool operator>=(const Rational& rValue) const;

bool operator<=(const Rational& rValue) const;

bool operator!=(const Rational& rValue) const;

const Rational operator+ (const Rational& rValue) const;

const Rational operator- (const Rational& rValue) const;

const Rational operator* (const Rational& rValue) const;

const Rational operator/ (const Rational& rValue) const;

const Rational operator^(long rValue) const; //whole power

const Rational operator^( const Rational& rValue) const; //Rational^ (optional)

Rational& operator+= (const Rational& rValue);

Rational& operator-= (const Rational& rValue);

Rational& operator*= (const Rational& rValue);

Rational& operator/= (const Rational& rValue);

string operator()() const;

operator double() const;

Rational& operator++();

Rational& operator--();

Rational operator++(int garbage);

Rational operator--(int garbage);

long getNumerator()const;

long getDenominator()const;

private:

long numerator;

long denominator;

long LeastCommonMultiple(long x, long y) const;

long GreatestCommonDivisor(long x, long y) const;

void Reduce();

//any other private functions you may need

};

//prototypes for non-member methods

ostream &operator<<(ostream&, const Rational& Fraction);

istream &operator>>(istream&, Rational& Fraction);

const Rational operator+ (const long lValue, const Rational & rValue);

const Rational operator- (const long lValue, const Rational & rValue);

const Rational operator* (const long lValue, const Rational & rValue);

const Rational operator/ (const long lValue, const Rational & rValue);

#endif

//Rational.cpp

#include"Rational.h"

#include<sstream>

Rational::Rational(long NewNumerator, long NewDenominator)

{

numerator = NewNumerator;

denominator = NewDenominator;

Reduce();

}

const Rational& Rational::operator=(const Rational & rValue)

{

numerator = rValue.numerator;

denominator = rValue.denominator;

Reduce();

return *this;

}

Rational& Rational::operator++()

{

numerator += denominator;

Reduce();

return *this;

}

Rational Rational::operator++(int Garbage)

{

Rational result = *this;

numerator += denominator;

Reduce();

return result;

}

bool Rational::operator==(const Rational & rValue) const

{

bool result = true;

if(numerator != rValue.numerator || denominator != rValue.denominator)

{

result = false;

}

return result;

}

const Rational Rational::operator+ (const Rational & rValue) const

{

long lcm = LeastCommonMultiple(rValue.denominator, denominator);

long resultNumerator = (numerator * (lcm/denominator)) +

(rValue.numerator * (lcm/rValue.denominator));

Rational result(resultNumerator,lcm);

return result;

}

Rational& Rational::operator+= (const Rational & rValue)

{

*this = *this + rValue;

Reduce();

return *this;

}

string Rational::operator()() const

{

std::stringstream stream;

stream << numerator << "/" << denominator;

return stream.str();

}

ostream &operator<<(ostream & out, const Rational & Fraction)

{

out << Fraction.getNumerator() << "\\" << Fraction.getDenominator();

return out;

}

long Rational::getNumerator()const

{

return numerator;

}

long Rational::getDenominator()const

{

return denominator;

}

long Rational::LeastCommonMultiple(long x, long y) const

{

bool Continue = true;

long result = x;

while (result % y != 0)

{

result += x;

}

return result;

}

long Rational::GreatestCommonDivisor(long x, long y) const

{

long remainder = x % y;

while(remainder != 0)

{

x = y;

y = remainder;

remainder = x % y;

}

return y;

}

void Rational::Reduce()

{

long GCD = GreatestCommonDivisor(numerator, denominator);

numerator = numerator/GCD;

denominator = denominator/GCD;

}