#include
#include "Rational .h"
using namespace std;
// By using the default parameter settings in Rational .h, this
// constructor also provides the default constructor Rational()
Rational::Rational(int num, int denom)
{
set Rational(num, denom); // set numerator and denominator, reduce fraction, fix the sign
}
// Helper function to fix a zero denominator and fix the sign if denominator is negative
Rational Rational::setRational(int n, int d) // helper function
{
numerator = n;
denominator = d;
// if denominator == 0 then set it = 1
if (denominator == 0)
denominator = 1;
if (denominator <>
{
numerator = -numerator; // fix sign of numerator +/-
denominator = -denominator; // denominator always +
}
int lcd = LCD(numerator, denominator);
if (denominator != 0)
{
numerator /= lcd;
denominator /= lcd;
}
return *this; // return the current object
}
// find the lowest common divisor using a recursive function
int Rational::LCD(int v1, int v2)
{
if (v2 == 0) return v1;
else return LCD(v2, v1%v2);
}
Rational Rational::add(Rational right)
{
int new Numerator;
int new Denominator;
New Numerator = numerator*right. denominator + right .numerator*denominator;
new Denominator = denominator * right .denominator;
// create a new Rational object and return it
return Rational(new Numerator, new Denominator);
}
// the operator+ method does the same thing as the add method
Rational Rational::operator+ (Rational right)
{
// create local (temporary) variables
int new Numerator;
int new Denominator;
// compute the result and save in the local variables
// the current object's numerator and denominator are not changed
new Numerator = numerator*right .denominator + right. numerator*denominator;
new Denominator = denominator * right .denominator;
// create a new Rational object with the result and return it
return Rational(new Numerator, new Denominator);
}
Rational Rational::operator+= (Rational right)
{
// the current object is updated with the result of the +=
numerator = numerator*right .denominator + right. numerator*denominator;
denominator = denominator * right .denominator;
// fix the sign, reduce the fraction and return the current object
return set Rational(numerator, denominator);
}
// the operator- method does the same thing as the add method
Rational Rational::operator- (Rational right)
{
// create local (temporary) variables
int new Numerator;
int new Denominator;
// compute the result and save in the local variables
// the current object's numerator and denominator are not changed
new Numerator = numerator*right .denominator – right .numerator*denominator;
new Denominator = denominator * right .denominator;
// create a new Rational object with the result and return it
return Rational(new Numerator, new Denominator);
}
Rational Rational::operator-= (Rational right)
{
// the current object is updated with the result of the -=
numerator = numerator*right .denominator – right .numerator*denominator;
denominator = denominator * right .denominator;
// fix the sign, reduce the fraction and return the current object
return set Rational(numerator, denominator);
}
Rational::operator double() const // convert Rational to double and return
{
return double(numerator) / double(denominator);
}
// Display a Rational number using the display() member method
void Rational::display()
{
count <><>
}
// Display a Rational number using <>
// Friend functions are not part of the class and their code must be
// declared outside of the class with no :: Scope Resolution Operator.
// All function arguments must have their class defined
o stream &operator<>
{
out <><><>
return out; // This is to keep the stream flowing
}
=====================================================
RationalTest.cpp
// Rational.cpp : Defines the entry point for the console application.
// Create a Rational class definition
// Rational(numerator, denominator)
//
#include "Rational. h" // double quotes = find file in project folder
#include // angle brackets = find file in compiler folder
using namespace std;
// function prototypes
void initialize Numbers(Rational &, Rational &, Rational &);
void display Numbers(const char *, Rational, Rational, Rational);
int main(int argc, char* argv[])
{
// class object
// | |
// V V
Rational n1;
Rational n2;
Rational n3;
cout <><><>
initialize Numbers(n1, n2, n3);
display Numbers("Before", n1, n2, n3);
n2 = n2.add(n3); // n2 + n3 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12
display Numbers("After ", n1, n2, n3);
count <>
n2.display(); // using the display( ) member function
count <>
count <><><><>
initialize Numbers(n1, n2, n3);
display Numbers("Before", n1, n2, n3);
n1 = n2.operator+(n3); // n2 + n3 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12
display Numbers("After ", n1, n2, n3);
count <><><><>
initialize Numbers(n1, n2, n3);
display Numbers("Before", n1, n2, n3);
n1 = n2 += n3;
display Numbers("After ", n1, n2, n3);
count <><><><>
initialize Numbers(n1, n2, n3);
display Numbers("Before", n1, n2, n3);
n1 = n2 - n3; // n2 - n3 = 3/4 - 2/3 = 9/12 - 8/12 = 1/12
display Numbers("After ", n1, n2, n3);
count <><><><>
initialize Numbers(n1, n2, n3);
display Numbers("Before", n1, n2, n3);
n1 = n2 -= n3;
display Numbers("After ", n1, n2, n3);
count <><><><>
count
count
system(">
return 0;
}
// Initialize each of the variables before testing each rational operator
void initialize Numbers(Rational &n1, Rational &n2, Rational &n3)
{
n1 = Rational(); // 0 no arguments
n2 = Rational(3, 4); // 3/4
n3 = Rational(2, 3); // 2/3
}
// Display each of the rational numbers using the friend function
void display Numbers(const char *msg, Rational n1, Rational n2, Rational n3)
{
count <><><><><><><><>
}
====================================================
Rational. h
#ifnde f RATIONAL_H // if this compiler macro is not defined
#define RATIONAL_H // then define it so this file will not be processed again
#include
using namespace std;
class Rational
{
// Friend functions are actually declared outside the scope of the
// class but have the right to access public and private data and
// member function members that belong to the class. The friend
// function below gives the <>
// the ability to output a Rational object by accessing its member data.
friend o stream &operator<>
public:
Rational(int num = 0, int de nom = 1); // also provides default constructor
Rational add(Rational right);
Rational operator+ (Rational right); // + addition operator
Rational operator+= (Rational right); // += addition assignment operator
Rational operator- (Rational right); // + addition operator
Rational operator-= (Rational right); // += addition assignment operator
void display();
operator double() const; // convert Rational to double
private:
int numerator;
int denominator;
// helper functions are private and not accessible by the main program
int LCD(int v1, int v2);
Rational set Rational(int n, int d);
};
#end if