Fraction Library
A C++ Fraction library.

Fraction Class Reference

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>

Inheritance diagram for Fraction:
MFraction

List of all members.

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).
Fractionoperator= (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.

Detailed Description

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:

  • a single number (can be integer or decimal value)
  • the numerator and denominator of the fraction to represent (integer values)
  • a valid string value (such as "5/6", "3.5", "4")
  • another Fraction object
  • see the constructor documentation below for details

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.


Member Enumeration Documentation

Enumeration type to track how objects of type Fraction should be outputted.

See also:
Fraction::FORMAT
Enumerator:
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.


Constructor & Destructor Documentation

Fraction::Fraction ( )

Default Constructor: sets fraction to 0.

This is the constructor that is used when no constructor is explicitely specifed.

Returns:
Fraction object.

Definition at line 90 of file Fraction.cpp.

Fraction::Fraction ( const double &  number)

Converts a decimal number (or integer) into a fraction.

Parameters:
numberDecimal number (or integer) to convert to fraction.
Returns:
Fraction object.

Definition at line 99 of file Fraction.cpp.

Fraction::Fraction ( const int &  numerator,
const int &  denominator 
)

Takes numerator and denominator of fraction.

Parameters:
numeratorNumerator of fraction.
denominatorDenominator of fraction.
Returns:
Fraction object.
Exceptions:
Ifdenominator 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.

Parameters:
fracCharacter 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".
Returns:
Fraction object.
Exceptions:
Ifinput 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.

Parameters:
fracFraction object to copy.
Returns:
Fraction object.

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.


Member Function Documentation

Fraction::FracFormat Fraction::getFormat ( ) [static]

Use to see what the current output format of fractions of type Fraction is.

Returns:
Returns current output format for fractions of type Fraction. This is a value of type Fraction::FracFormat.

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.

Returns:
Returns the GCD of the numerator and denominator. If numerator of fraction is equal to 0, method returns 0.

Definition at line 49 of file Fraction.cpp.

Fraction Fraction::operator*= ( const Fraction right)

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.

Parameters:
rightFraction 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.
Returns:
Returns a fraction object that is equal to the value of the updated calling object.

Definition at line 305 of file Fraction.cpp.

Fraction Fraction::operator++ ( )

Prefix increment operator: adds 1 to fraction.

Returns:
Returns the updated fraction object so that c = ++b is possible, (here 1 is added to fraction and then b is assigned to c).
Note:
If returned reference instead, in cases such as ++d = b, 1 would be added to d and then assigned value of b.

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.

Parameters:
inc
  • Implicit - c++ : increments fraction by 1 (inc will be equal to 0)
  • Explicit - Fraction::operator ++ (value) : increments fraction by value (inc = value)
Returns:
Returns the updated fraction object so that c = b++ is possible, (here, b is assigned to c and then 1 is added to b).
Note:
If returned reference instead, in cases such as d++ = b, b's value would be assigned to d and then 1 would be added to d.
Exceptions:
Ifinc 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.

Fraction Fraction::operator+= ( const Fraction right)

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.

Parameters:
rightFraction 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.
Returns:
Returns a fraction object that is equal to the value of the updated calling object.

Definition at line 289 of file Fraction.cpp.

Fraction Fraction::operator- ( ) const

Negation operator: returns the negated value of the calling Fraction object.

Note:
Doesn't change the value of the calling object. The calling object must be of type Fraction.
Returns:
Returns the negated value of the calling Fraction object. Note that operation doesn't return a reference. ie. -b=c, b wont be assigned to c.

Reimplemented in MFraction.

Definition at line 231 of file Fraction.cpp.

Fraction Fraction::operator-- ( )

Prefix decrement operator: subtracts 1 from fraction.

Returns:
Returns the updated fraction object so that c = --b is possible, (here 1 is subtracted from fraction and then b is assigned to c).
Note:
If returned reference instead, in cases such as --d = b, 1 would be subtracted and then assigned value of b.

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.

Parameters:
dec
  • Implicit - c-- : decrements fraction by 1 (dec will be equal to 0)
  • Explicit - Fraction::operator -- (value) : decrements fraction by value (dec = value)
Returns:
Returns the updated fraction object so that c = b-- is possible, (here, b is assigned to c and then 1 is subtracted from b).
Note:
If returned reference instead, in cases such as d-- = b, b's value would be assigned to d and then 1 would be subtracted from d.
Exceptions:
Ifdec 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.

Fraction Fraction::operator-= ( const Fraction right)

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.

Parameters:
rightFraction 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.
Returns:
Returns a fraction object that is equal to the value of the updated calling object.

Definition at line 297 of file Fraction.cpp.

Fraction Fraction::operator/= ( const Fraction right)

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.

Parameters:
rightFraction 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.
Returns:
Returns a fraction object that is equal to the value of the updated calling object.

Definition at line 313 of file Fraction.cpp.

Fraction & Fraction::operator= ( const Fraction right)

Assignment operator: Calling object is made equal to object on the right of operator.

This operator must be declared as a member function.

Parameters:
rightFraction object on the right of the equal sign. Automatic type conversions are made when values of integer, double or string is provided.
Returns:
Returns reference to object to the left of the equal sign. This makes it possible to do x = y = z.

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.

Parameters:
subscriptvalue of 0 or 1. Any other value will throw an exception.
Returns:
Returns an integer value (not a reference)
  • numerator (signed) if subscript = 0
  • denominator (unsigned) if subscript = 1
Exceptions:
ThrowsFR_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.

Parameters:
denominatorThe number to set the denomerator of the fraction to.
Exceptions:
ThrowsFR_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.

Parameters:
formatWhat 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.

Parameters:
numeratorThe number to set the numerator of the fraction to.

Reimplemented in MFraction.

Definition at line 182 of file Fraction.cpp.


Friends And Related Function Documentation

bool operator!= ( const Fraction left,
const Fraction right 
) [friend]

Inequality operator: checks if two fractions arent equal.

Parameters:
leftFraction object to the left of the operator.
rightFraction object to the right of the operator.
Returns:
Returns true if the two fraction objects aren't equal. False otherwise.

Definition at line 328 of file Fraction.cpp.

Fraction operator* ( const Fraction left,
const Fraction right 
) [friend]

Multiplication operator: multiplies the two fractions on either side of the operator.

Parameters:
leftFraction on the left of the operator
rightFraction on the right of the operator
Returns:
Returns the reduced product of the two fractions.

Definition at line 370 of file Fraction.cpp.

Fraction operator+ ( const Fraction left,
const Fraction right 
) [friend]

Addition operator: adds the two fractions on either side of the operator.

Parameters:
leftFraction on the left of the operator
rightFraction on the right of the operator
Returns:
Returns the reduced sum of the two fractions.

Definition at line 358 of file Fraction.cpp.

Fraction operator- ( const Fraction left,
const Fraction right 
) [friend]

Subtraction operator: subtracts the two fractions on either side of the operator.

Parameters:
leftFraction on the left of the operator
rightFraction on the right of the operator
Returns:
Returns the reduced difference of the two fractions.

Definition at line 364 of file Fraction.cpp.

Fraction operator/ ( const Fraction left,
const Fraction right 
) [friend]

Division operator: left fraction is divided by the fraction on the right of the operator.

Parameters:
leftFraction on the left of the operator
rightFraction on the right of the operator
Returns:
Returns the reduced quotient of the two fractions.

Definition at line 376 of file Fraction.cpp.

bool operator< ( const Fraction left,
const Fraction right 
) [friend]

Less than operator: checks if fraction on the left of operator is less than fraction on the right.

Parameters:
leftFraction object to the left of the operator.
rightFraction object to the right of the operator.
Returns:
Returns true if left fraction is less than right fraction. False otherwise.

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.

Parameters:
outoutput stream (operation assumes stream is already setup (ie. connected to file, screen etc.)
fractionFraction object to output.
Returns:
Returns reference to output stream so that for instance, cout << x << y is possible.

Definition at line 384 of file Fraction.cpp.

bool operator<= ( const Fraction left,
const Fraction right 
) [friend]

Less than or equal to operator: checks if fraction on the left of operator is less than or equal to fraction on the right.

Parameters:
leftFraction object to the left of the operator.
rightFraction object to the right of the operator.
Returns:
Returns true if left fraction is less than or equal to right fraction. False otherwise.

Definition at line 340 of file Fraction.cpp.

bool operator== ( const Fraction left,
const Fraction right 
) [friend]

Equality operator: checks if two fractions are same.

Parameters:
leftFraction object to the left of the operator.
rightFraction object to the right of the operator.
Returns:
Returns true if the two fraction objects are equal. False otherwise.

Definition at line 322 of file Fraction.cpp.

bool operator> ( const Fraction left,
const Fraction right 
) [friend]

Greater than operator: checks if fraction on the left of operator is greater than fraction on the right.

Parameters:
leftFraction object to the left of the operator.
rightFraction object to the right of the operator.
Returns:
Returns true if left fraction is greater than right fraction. False otherwise.

Definition at line 346 of file Fraction.cpp.

bool operator>= ( const Fraction left,
const Fraction right 
) [friend]

Greater than or equal to operator: checks if fraction on the left of operator is greater than or equal to fraction on the right.

Parameters:
leftFraction object to the left of the operator.
rightFraction object to the right of the operator.
Returns:
Returns true if left fraction is greater than or equal to right fraction. False otherwise.

Definition at line 352 of file Fraction.cpp.

istream& operator>> ( istream &  in,
Fraction fraction 
) [friend]

Stream input: assign input to a fraction object.

Parameters:
ininput stream (operation assumes stream is already setup (ie. connected to file, screen etc.)
fractionThe input from stream is assigned to this fraction object.
Returns:
Returns reference to input stream so that for instance, cin >> x >> y is possible.

Definition at line 399 of file Fraction.cpp.


Member Data Documentation

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.

See also:
Fraction::setFormat(), Fraction::getFormat()

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.


The documentation for this class was generated from the following files: