Fraction Library
A C++ Fraction library.

This class allows you to handle improper fractions just like how basic types such as integers and doubles are handled in c++. More...
#include <Fraction.h>
Public Types  
enum  FracFormat { IM_FRAC = 0, DECI = 1 } 
Enumeration type to track how objects of type Fraction should be outputted. More...  
Public Member Functions  
Constructors/Destructor  
A constructor initilizes (creates) an object of type Fraction. A destructor destroys an object of type Fraction when it goes out of scope.  
Fraction ()  
Default Constructor: sets fraction to 0.  
Fraction (const double &number)  
Converts a decimal number (or integer) into a fraction.  
Fraction (const int &numerator, const int &denominator)  
Takes numerator and denominator of fraction.  
Fraction (const char *frac)  
Converts a valid character array into a fraction.  
Fraction (const Fraction &frac)  
Copy Constructor: copies a fraction object into the one being initilized.  
~Fraction ()  
Destructor: used to destory objects of type Fraction.  
Mutator Methods  
Use these methods to set the numerator/denominator independently. (to get the values of the numerator and denominator, see Fraction::operator [])  
void  setNum (int numerator) 
Sets the numerator of the fraction.  
void  setDen (int denominator) 
Sets the denomerator of the fraction.  
Member Overloaded Operators  
The following are overloaded operators which are members of the class. One of the "parameters" to the operation is implicit and is the "calling object". The calling object must be of type Fraction. For instance, x[0] and x, where x is the calling object.  
int  operator[] (const unsigned int &subscript) const 
Subscript operator: Pass in 0 to get numerator (signed), 1 for denominator (unsigned).  
Fraction &  operator= (const Fraction &right) 
Assignment operator: Calling object is made equal to object on the right of operator.  
Fraction  operator () const 
Negation operator: returns the negated value of the calling Fraction object.  
Fraction  operator++ () 
Prefix increment operator: adds 1 to fraction.  
Fraction  operator () 
Prefix decrement operator: subtracts 1 from fraction.  
Fraction  operator++ (int inc) 
Postfix increment operator: increments fraction by inc; if inc is 0, increments by 1.  
Fraction  operator (int dec) 
Postfix decrement operator: decrements fraction by dec; if dec is 0, decrements by 1.  
Fraction  operator+= (const Fraction &right) 
Addition/assignment operator: adds fraction on the right of operator to fraction on the left.  
Fraction  operator= (const Fraction &right) 
Subtraction/assignment operator: subtracts fraction on the right of operator from fraction on the left.  
Fraction  operator*= (const Fraction &right) 
Multiplication/assignment operator: calling object is multiplied by fraction on the right of the operator.  
Fraction  operator/= (const Fraction &right) 
Divition/assignment operator: calling object is divided by fraction on the right of the operator.  
Static Public Member Functions  
static void  setFormat (const FracFormat &format) 
Sets the format of how objects of type Fraction is outputted.  
static FracFormat  getFormat () 
Use to see what the current output format of fractions of type Fraction is.  
Protected Attributes  
bool  sign 
Stores sign of fraction.  
unsigned int  numerator 
Stores the numerator of the fraction.  
unsigned int  denominator 
Stores the denominator of the fraction.  
Private Member Functions  
unsigned int  getGCD () const 
Gets the Greatest Common Divisor (GCD) of the numerator and denominator.  
void  reduce () 
Converts a fraction into its reduced improper form.  
Static Private Attributes  
static FracFormat  FORMAT = IM_FRAC 
Stores how objects of type Fraction is to be outputted.  
Friends  
Comparison Operations  
These are declared as friends so that it is possible to 25 < x or 5==x where x is a Fraction object. Parameters are automatically converted to Fraction objects when values of integer, double or string are provided. However, note that at least one of the parameters must be an object of type Fraction.  
bool  operator== (const Fraction &left, const Fraction &right) 
Equality operator: checks if two fractions are same.  
bool  operator!= (const Fraction &left, const Fraction &right) 
Inequality operator: checks if two fractions arent equal.  
bool  operator< (const Fraction &left, const Fraction &right) 
Less than operator: checks if fraction on the left of operator is less than fraction on the right.  
bool  operator<= (const Fraction &left, const Fraction &right) 
Less than or equal to operator: checks if fraction on the left of operator is less than or equal to fraction on the right.  
bool  operator> (const Fraction &left, const Fraction &right) 
Greater than operator: checks if fraction on the left of operator is greater than fraction on the right.  
bool  operator>= (const Fraction &left, const Fraction &right) 
Greater than or equal to operator: checks if fraction on the left of operator is greater than or equal to fraction on the right.  
Arithmitic Operations  
These are declared as friends so that it is possible to 25 + x or 5/x where x is a Fraction object. Parameters are automatically converted to Fraction objects when values of integer, double or string are provided. However, note that at least one of the parameters must be an object of type Fraction.  
Fraction  operator+ (const Fraction &left, const Fraction &right) 
Addition operator: adds the two fractions on either side of the operator.  
Fraction  operator (const Fraction &left, const Fraction &right) 
Subtraction operator: subtracts the two fractions on either side of the operator.  
Fraction  operator* (const Fraction &left, const Fraction &right) 
Multiplication operator: multiplies the two fractions on either side of the operator.  
Fraction  operator/ (const Fraction &left, const Fraction &right) 
Division operator: left fraction is divided by the fraction on the right of the operator.  
Stream input/output operations  
ostream &  operator<< (ostream &out, const Fraction &fraction) 
Stream output: output fraction in the format specified by Fraction::FORMAT.  
istream &  operator>> (istream &in, Fraction &fraction) 
Stream input: assign input to a fraction object. 
This class allows you to handle improper fractions just like how basic types such as integers and doubles are handled in c++.
Improper fractions are of the form numerator/denominator. For instance, 5/6 or 10/3. These fractions will always be in their reduced form.
The programmer has a choice of outputting the fraction as a decimal number or as a fraction. This applies to all fraction objects that are of type Fraction. For instance, 3.5 or 7/2. See documentation of "Static Public Member Functions" for details.
The programmer can initilize fraction objects by providing:
Arithmetic and comparison operations can also be performed on Fraction objects, analogous to how these operations work on values of basic types such as an int.
I/O with Fractions works similarly to how it works with integers as well. For instance, 2/3, 8/4 and 5.6 are all valid inputs. These inputs will be stored in their reduced form.
Definition at line 42 of file Fraction.h.
enum Fraction::FracFormat 
Enumeration type to track how objects of type Fraction should be outputted.
IM_FRAC 
= 0: output as improper fractions ie. 5/6, 8/5 
DECI 
= 1: output as a decimal number ie. 3.5 
Definition at line 50 of file Fraction.h.
Fraction::Fraction  (  ) 
Default Constructor: sets fraction to 0.
This is the constructor that is used when no constructor is explicitely specifed.
Definition at line 90 of file Fraction.cpp.
Fraction::Fraction  (  const double &  number  ) 
Converts a decimal number (or integer) into a fraction.
number  Decimal number (or integer) to convert to fraction. 
Definition at line 99 of file Fraction.cpp.
Fraction::Fraction  (  const int &  numerator, 
const int &  denominator  
) 
Takes numerator and denominator of fraction.
numerator  Numerator of fraction. 
denominator  Denominator of fraction. 
If  denominator is zero, throws FR_ERROR with value FR_DENOM_ZERO. Object is left uncreated (also means destructor will not run as well). 
Definition at line 114 of file Fraction.cpp.
Fraction::Fraction  (  const char *  frac  ) 
Converts a valid character array into a fraction.
frac  Character array to convert. Possible values include "3.4" and "7/8". Invalid values include "3.4.5", "1/2/3" and "3.4/5.6". 
If  input is invalid, throws FR_ERROR with value FR_STR_INVALID. Object is left uncreated (also means destructor will not run as well). 
Definition at line 130 of file Fraction.cpp.
Fraction::Fraction  (  const Fraction &  frac  ) 
Copy Constructor: copies a fraction object into the one being initilized.
frac  Fraction object to copy. 
Definition at line 165 of file Fraction.cpp.
Fraction::~Fraction  (  ) 
Destructor: used to destory objects of type Fraction.
This cannot be called explicitely.
Definition at line 175 of file Fraction.cpp.
Fraction::FracFormat Fraction::getFormat  (  )  [static] 
Use to see what the current output format of fractions of type Fraction is.
Reimplemented in MFraction.
Definition at line 39 of file Fraction.cpp.
unsigned int Fraction::getGCD  (  )  const [private] 
Gets the Greatest Common Divisor (GCD) of the numerator and denominator.
Definition at line 49 of file Fraction.cpp.
Multiplication/assignment operator: calling object is multiplied by fraction on the right of the operator.
Calling object must be of type Fraction. The calling object is the fraction that is on the left of the operator.
right  Fraction object on the right of operator that is to be multiplied with. If values of integer, double or strings are provided, they will be automatically converted to Fraction objects prior to multiplication/assignment. 
Definition at line 305 of file Fraction.cpp.
Fraction Fraction::operator++  (  ) 
Prefix increment operator: adds 1 to fraction.
Reimplemented in MFraction.
Definition at line 238 of file Fraction.cpp.
Fraction Fraction::operator++  (  int  inc  ) 
Postfix increment operator: increments fraction by inc; if inc is 0, increments by 1.
inc 

If  inc is a negative value, throws FR_ERROR with value FR_NEG_PARAM. Calling object remains unchanged. 
Reimplemented in MFraction.
Definition at line 250 of file Fraction.cpp.
Addition/assignment operator: adds fraction on the right of operator to fraction on the left.
Calling object must be of type Fraction. The calling object is the fraction that is on the left of the operator.
right  Fraction object on the right of operator that is to be added. If values of integer, double or strings are provided, they will be automatically converted to Fraction objects prior to addition/assignment. 
Definition at line 289 of file Fraction.cpp.
Fraction Fraction::operator  (  )  const 
Negation operator: returns the negated value of the calling Fraction object.
Reimplemented in MFraction.
Definition at line 231 of file Fraction.cpp.
Fraction Fraction::operator  (  ) 
Prefix decrement operator: subtracts 1 from fraction.
Reimplemented in MFraction.
Definition at line 244 of file Fraction.cpp.
Fraction Fraction::operator  (  int  dec  ) 
Postfix decrement operator: decrements fraction by dec; if dec is 0, decrements by 1.
dec 

If  dec is a negative value, throws FR_ERROR with value FR_NEG_PARAM. Calling object remains unchanged. 
Reimplemented in MFraction.
Definition at line 269 of file Fraction.cpp.
Subtraction/assignment operator: subtracts fraction on the right of operator from fraction on the left.
Calling object must be of type Fraction. The calling object is the fraction that is on the left of the operator.
right  Fraction object on the right of operator that is to be subtracted. If values of integer, double or strings are provided, they will be automatically converted to Fraction objects prior to subtraction/assignment. 
Definition at line 297 of file Fraction.cpp.
Divition/assignment operator: calling object is divided by fraction on the right of the operator.
Calling object must be of type Fraction. The calling object is the fraction that is on the left of the operator.
right  Fraction object on the right of operator that is to be divided by. If values of integer, double or strings are provided, they will be automatically converted to Fraction objects prior to division/assignment. 
Definition at line 313 of file Fraction.cpp.
Assignment operator: Calling object is made equal to object on the right of operator.
This operator must be declared as a member function.
right  Fraction object on the right of the equal sign. Automatic type conversions are made when values of integer, double or string is provided. 
Definition at line 216 of file Fraction.cpp.
int Fraction::operator[]  (  const unsigned int &  subscript  )  const 
Subscript operator: Pass in 0 to get numerator (signed), 1 for denominator (unsigned).
This operator must be a member function.
subscript  value of 0 or 1. Any other value will throw an exception. 
Throws  FR_ERROR with value FR_INDEX_OUT_BOUNDS if subscript is greater than 1. 
Reimplemented in MFraction.
Definition at line 202 of file Fraction.cpp.
void Fraction::reduce  (  )  [private] 
Converts a fraction into its reduced improper form.
For instance, 6/3 > 2 and 8/6 > 4/3
Reimplemented in MFraction.
Definition at line 74 of file Fraction.cpp.
void Fraction::setDen  (  int  denominator  ) 
Sets the denomerator of the fraction.
denominator  The number to set the denomerator of the fraction to. 
Throws  FR_ERROR with value FR_DENOM_ZERO if parameter's value is 0. 
Reimplemented in MFraction.
Definition at line 190 of file Fraction.cpp.
void Fraction::setFormat  (  const FracFormat &  format  )  [static] 
Sets the format of how objects of type Fraction is outputted.
format  What the format of the fraction should be:

Reimplemented in MFraction.
Definition at line 33 of file Fraction.cpp.
void Fraction::setNum  (  int  numerator  ) 
Sets the numerator of the fraction.
numerator  The number to set the numerator of the fraction to. 
Reimplemented in MFraction.
Definition at line 182 of file Fraction.cpp.
Inequality operator: checks if two fractions arent equal.
left  Fraction object to the left of the operator. 
right  Fraction object to the right of the operator. 
Definition at line 328 of file Fraction.cpp.
Multiplication operator: multiplies the two fractions on either side of the operator.
left  Fraction on the left of the operator 
right  Fraction on the right of the operator 
Definition at line 370 of file Fraction.cpp.
Addition operator: adds the two fractions on either side of the operator.
left  Fraction on the left of the operator 
right  Fraction on the right of the operator 
Definition at line 358 of file Fraction.cpp.
Subtraction operator: subtracts the two fractions on either side of the operator.
left  Fraction on the left of the operator 
right  Fraction on the right of the operator 
Definition at line 364 of file Fraction.cpp.
Division operator: left fraction is divided by the fraction on the right of the operator.
left  Fraction on the left of the operator 
right  Fraction on the right of the operator 
Definition at line 376 of file Fraction.cpp.
Less than operator: checks if fraction on the left of operator is less than fraction on the right.
left  Fraction object to the left of the operator. 
right  Fraction object to the right of the operator. 
Definition at line 334 of file Fraction.cpp.
ostream& operator<<  (  ostream &  out, 
const Fraction &  fraction  
)  [friend] 
Stream output: output fraction in the format specified by Fraction::FORMAT.
out  output stream (operation assumes stream is already setup (ie. connected to file, screen etc.) 
fraction  Fraction object to output. 
Definition at line 384 of file Fraction.cpp.
Less than or equal to operator: checks if fraction on the left of operator is less than or equal to fraction on the right.
left  Fraction object to the left of the operator. 
right  Fraction object to the right of the operator. 
Definition at line 340 of file Fraction.cpp.
Equality operator: checks if two fractions are same.
left  Fraction object to the left of the operator. 
right  Fraction object to the right of the operator. 
Definition at line 322 of file Fraction.cpp.
Greater than operator: checks if fraction on the left of operator is greater than fraction on the right.
left  Fraction object to the left of the operator. 
right  Fraction object to the right of the operator. 
Definition at line 346 of file Fraction.cpp.
Greater than or equal to operator: checks if fraction on the left of operator is greater than or equal to fraction on the right.
left  Fraction object to the left of the operator. 
right  Fraction object to the right of the operator. 
Definition at line 352 of file Fraction.cpp.
istream& operator>>  (  istream &  in, 
Fraction &  fraction  
)  [friend] 
Stream input: assign input to a fraction object.
in  input stream (operation assumes stream is already setup (ie. connected to file, screen etc.) 
fraction  The input from stream is assigned to this fraction object. 
Definition at line 399 of file Fraction.cpp.
unsigned int Fraction::denominator [protected] 
Stores the denominator of the fraction.
Definition at line 90 of file Fraction.h.
Fraction::FracFormat Fraction::FORMAT = IM_FRAC [static, private] 
Stores how objects of type Fraction is to be outputted.
This applies to all objects of type Fraction. The default behaviour is as an improper fraction.
Reimplemented in MFraction.
Definition at line 61 of file Fraction.h.
unsigned int Fraction::numerator [protected] 
Stores the numerator of the fraction.
Definition at line 89 of file Fraction.h.
bool Fraction::sign [protected] 
Stores sign of fraction.
Value of true for positive fractions. False for negative.
Definition at line 88 of file Fraction.h.