Fraction Library
A C++ Fraction library.

MFraction Class Reference

This class allows you to handle improper as well as mixed fractions just like how basic types such as integers and doubles are handled in c++. More...

#include <MFraction.h>

Inheritance diagram for MFraction:
Fraction

List of all members.

Public Member Functions

Constructors/Destructor

A constructor initilizes (creates) an object of type MFraction. A destructor destroys an object of type MFraction when it goes out of scope.

 MFraction ()
 Default Constructor: sets fraction to 0.
 MFraction (const double &number)
 Converts a decimal number (or integer) into a fraction.
 MFraction (const int &numerator, const int &denominator)
 Takes numerator and denominator of fraction (not reduced) and creates a reduced mixed fraction.
 MFraction (const int &whole, const int &numerator, const int &denominator)
 Takes whole, numerator and denominator of fraction (not reduced) and creates a reduced mixed fraction.
 MFraction (const char *frac)
 Converts a valid character array into a fraction.
 MFraction (const Fraction &frac)
 Copy Constructor: copies another fraction (of type Fraction) into the created fraction (of type MFraction).
 MFraction (const MFraction &frac)
 Copy Constructor: copies another fraction (of type MFraction) into the created fraction (of type MFraction).
 ~MFraction ()
 Destructor: used to destory objects of type MFraction.
Mutator Methods

Use these methods to set the whole/numerator/denominator independently.

(to get the values of the whole, numerator and denominator, see MFraction::operator [])

void setWhole (int whole)
 Sets the whole number portion of the fraction.
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 MFraction. 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 whole (signed), 1 to get numerator (signed), 2 for denominator (unsigned).
MFractionoperator= (const MFraction &right)
 Assignment operator: Calling object is made equal to object on the right of operator.
MFraction operator- () const
 Negation operator: returns the negated value of the calling MFraction object.
MFraction operator++ ()
 Prefix increment operator: adds 1 to fraction.
MFraction operator-- ()
 Prefix decrement operator: subtracts 1 from fraction.
MFraction operator++ (int inc)
 Postfix increment operator: increments fraction by inc; if inc is 0, increments by 1.
MFraction operator-- (int dec)
 Postfix decrement operator: decrements fraction by dec; if dec is 0, decrements by 1.
MFraction operator+= (const MFraction &right)
 Addition/assignment operator: adds fraction on the right of operator to fraction on the left.
MFraction operator-= (const MFraction &right)
 Subtraction/assignment operator: subtracts fraction on the right of operator from fraction on the left.
MFraction operator*= (const MFraction &right)
 Multiplication/assignment operator: calling object is multiplied by fraction on the right of the operator.
MFraction operator/= (const MFraction &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 MFraction is outputted.
static FracFormat getFormat ()
 Use to see what the current output format of fractions of type MFraction is.

Private Member Functions

void reduce ()
 Reduces a reduced improper fraction into a mixed fraction.

Private Attributes

__pad0__:/Users/EnemyUnited/My Stuff/Fraction Library 1.1/include/MFraction.h" 2 public: enum FracFormat { MIX_FRAC = 0
 = 0: output as mixed fraction when it applies, ie 5/6 and 1/2/3
IM_FRAC = 1
 = 1: output as improper fractions ie. 5/6, 8/5
DECI
 = 2: output as a decimal number ie. 3.5
unsigned int whole
 Stores the whole number portion of the fraction.

Static Private Attributes

static FracFormat FORMAT = MIX_FRAC
 Stores how objects of type MFraction 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 MFraction object.

Parameters are automatically converted to MFraction objects when values of integer, double, string or Fraction objects are provided. However, note that at least one of the parameters must be an object of type MFraction.

bool operator== (const MFraction &left, const MFraction &right)
 Equality operator: checks if two fractions are same.
bool operator!= (const MFraction &left, const MFraction &right)
 Inequality operator: checks if two fractions arent equal.
bool operator< (const MFraction &left, const MFraction &right)
 Less than operator: checks if fraction on the left of operator is less than fraction on the right.
bool operator<= (const MFraction &left, const MFraction &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 MFraction &left, const MFraction &right)
 Greater than operator: checks if fraction on the left of operator is greater than fraction on the right.
bool operator>= (const MFraction &left, const MFraction &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 MFraction object.

Parameters are automatically converted to MFraction objects when values of integer, double, string or Fraction objects are provided. However, note that at least one of the parameters must be an object of type MFraction.

MFraction operator+ (const MFraction &left, const MFraction &right)
 Addition operator: adds the two fractions on either side of the operator.
MFraction operator- (const MFraction &left, const MFraction &right)
 Subtraction operator: subtracts the two fractions on either side of the operator.
MFraction operator* (const MFraction &left, const MFraction &right)
 Multiplication operator: multiplies the two fractions on either side of the operator.
MFraction operator/ (const MFraction &left, const MFraction &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 MFraction &fraction)
 Stream output: output fraction in the format specified by MFraction::FORMAT.
istream & operator>> (istream &in, MFraction &fraction)
 Stream input: assign input to a fraction object.

Detailed Description

This class allows you to handle improper as well as mixed fractions just like how basic types such as integers and doubles are handled in c++.

Improper fractions are of the form numerator/denominator whereas mixed fractions of the form whole/numerator/denominator where numerator in this case is always less than the denominator. For instance, 5/6 or 4/2/3. These fractions will always be in their reduced form.

The programmer has a choice of outputting the fraction as a decimal number, as an improper fraction or as a mixed fraction. This applies to all fraction objects that are of type MFraction. For instance, 3.5 or 7/2 or 3/1/2. See documentation of "Static Public Member Functions" for details.

The programmer can initilize fraction objects by providing (note the integer or string values provided do not have to be in their reduced fractional form):

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

Arithmetic and comparison operations can also be performed on MFraction objects, analogous to how these operations work on values of basic types such as an int.

I/O with MFractions works similarly to how it works with integers as well. For instance, 6/4/2, 2/3, 8/4 and 5.6 are all valid inputs. These inputs will be stored in their reduced form.

Definition at line 44 of file MFraction.h.


Constructor & Destructor Documentation

MFraction::MFraction ( )

Default Constructor: sets fraction to 0.

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

Returns:
MFraction object.

Definition at line 48 of file MFraction.cpp.

MFraction::MFraction ( const double &  number)

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

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

Definition at line 55 of file MFraction.cpp.

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

Takes numerator and denominator of fraction (not reduced) and creates a reduced mixed fraction.

Parameters:
numeratorNumerator of fraction.
denominatorDenominator of fraction.
Returns:
MFraction 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 62 of file MFraction.cpp.

MFraction::MFraction ( const int &  whole,
const int &  numerator,
const int &  denominator 
)

Takes whole, numerator and denominator of fraction (not reduced) and creates a reduced mixed fraction.

Parameters:
wholeWhole number portion of the fraction.
numeratorNumerator of fraction.
denominatorDenominator of fraction.
Returns:
MFraction 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 69 of file MFraction.cpp.

MFraction::MFraction ( const char *  frac)

Converts a valid character array into a fraction.

Parameters:
fracCharacter array to convert. Possible values include "3.4", "4/7/3" and "16/8". Invalid values include "3.4.5" and "3.4/5.6".
Returns:
MFraction 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 77 of file MFraction.cpp.

MFraction::MFraction ( const Fraction frac)

Copy Constructor: copies another fraction (of type Fraction) into the created fraction (of type MFraction).

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

Definition at line 117 of file MFraction.cpp.

MFraction::MFraction ( const MFraction frac)

Copy Constructor: copies another fraction (of type MFraction) into the created fraction (of type MFraction).

Parameters:
fracMFraction object to copy.
Returns:
MFraction object.

Definition at line 124 of file MFraction.cpp.

MFraction::~MFraction ( )

Destructor: used to destory objects of type MFraction.

This cannot be called explicitely.

Definition at line 131 of file MFraction.cpp.


Member Function Documentation

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

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

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

Reimplemented from Fraction.

Definition at line 26 of file MFraction.cpp.

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

Multiplication/assignment operator: calling object is multiplied by fraction on the right of the operator.

Calling object must be of type MFraction. The calling object is the fraction that is on the left of the operator.

Parameters:
rightMFraction object on the right of operator that is to be multiplied with. If values of integer, double, strings or Fraction are provided, they will be automatically converted to MFraction objects prior to multiplication/assignment.
Returns:
Returns a fraction object that is equal to the value of the updated calling object.

Definition at line 255 of file MFraction.cpp.

MFraction MFraction::operator++ ( )

Prefix increment operator: adds 1 to fraction.

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

Reimplemented from Fraction.

Definition at line 190 of file MFraction.cpp.

MFraction MFraction::operator++ ( int  inc)

Postfix increment operator: increments fraction by inc; if inc is 0, increments by 1.

Parameters:
inc
  • Implicit - c++ : increments numerator by 1 (inc will be equal to 0)
  • Explicit - MFraction::operator ++ (value) : increments numerator by value (inc = value)
Returns:
Returns the updated fraction object so that c = b++ is possible, (here, b is assigned to c and then b is incremented by 1).
Note:
If returned reference instead, in cases such as d++ = b, b's value would be assigned to d and then d's numerator would be incremented by 1.

Reimplemented from Fraction.

Definition at line 202 of file MFraction.cpp.

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

Addition/assignment operator: adds fraction on the right of operator to fraction on the left.

Calling object must be of type Mraction. The calling object is the fraction that is on the left of the operator.

Parameters:
rightMFraction 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 MFraction objects prior to addition/assignment.
Returns:
Returns a MFraction object that is equal to the value of the updated calling object.

Definition at line 239 of file MFraction.cpp.

MFraction MFraction::operator- ( ) const

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

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

Reimplemented from Fraction.

Definition at line 183 of file MFraction.cpp.

MFraction MFraction::operator-- ( )

Prefix decrement operator: subtracts 1 from fraction.

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

Reimplemented from Fraction.

Definition at line 196 of file MFraction.cpp.

MFraction MFraction::operator-- ( int  dec)

Postfix decrement operator: decrements fraction by dec; if dec is 0, decrements by 1.

Parameters:
dec
  • Implicit - c-- : decrements numerator by 1 (dec will be equal to 0)
  • Explicit - MFraction::operator -- (value) : decrements numerator by value (dec = value)
Returns:
Returns the updated fraction object so that c = b-- is possible, (here, b is assigned to c and then b is decremented by 1).
Note:
If returned reference instead, in cases such as d-- = b, b's value would be assigned to d and then d's numerator would be decremented by 1.

Reimplemented from Fraction.

Definition at line 220 of file MFraction.cpp.

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

Subtraction/assignment operator: subtracts fraction on the right of operator from fraction on the left.

Calling object must be of type MFraction. The calling object is the fraction that is on the left of the operator.

Parameters:
rightMFraction object on the right of operator that is to be subtracted. If values of integer, double, strings or Fraction are provided, they will be automatically converted to MFraction objects prior to subtraction/assignment.
Returns:
Returns a MFraction object that is equal to the value of the updated calling object.

Definition at line 247 of file MFraction.cpp.

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

Divition/assignment operator: calling object is divided by fraction on the right of the operator.

Calling object must be of type MFraction. The calling object is the fraction that is on the left of the operator.

Parameters:
rightMFraction object on the right of operator that is to be divided by. If values of integer, double, strings or Fraction 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 263 of file MFraction.cpp.

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

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

If a Fraction object is on the right of the operator, it will be automatically converted to a MFraction (using the available constructor) and then assigned to the calling object. This wont work if a MFraction object is being assigned to a Fraction object (the whole portion will be removed in that case).

Definition at line 175 of file MFraction.cpp.

int MFraction::operator[] ( const unsigned int &  subscript) const

Subscript operator: Pass in 0 to get whole (signed), 1 to get numerator (signed), 2 for denominator (unsigned).

This operator must be a member function.

Parameters:
subscriptvalue of 0, 1 or 2. Any other value will throw an exception.
Returns:
Returns an integer value (not a reference)
  • whole (signed) if subscript = 0
  • numerator (signed) if subscript = 1
  • denominator (unsigned) if subscript = 2
Exceptions:
throwsFR_ERROR with value FR_INDEX_OUT_BOUNDS if subscript is greater than 2.

Reimplemented from Fraction.

Definition at line 162 of file MFraction.cpp.

void MFraction::reduce ( ) [private]

Reduces a reduced improper fraction into a mixed fraction.

For instance, 6/5 -> 1/1/5

See also:
Fraction::reduce()

Reimplemented from Fraction.

Definition at line 34 of file MFraction.cpp.

void MFraction::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 from Fraction.

Definition at line 152 of file MFraction.cpp.

void MFraction::setFormat ( const FracFormat format) [static]

Sets the format of how objects of type MFraction is outputted.

Parameters:
formatWhat the format of the fraction should be:

Reimplemented from Fraction.

Definition at line 20 of file MFraction.cpp.

void MFraction::setNum ( int  numerator)

Sets the numerator of the fraction.

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

Reimplemented from Fraction.

Definition at line 145 of file MFraction.cpp.

void MFraction::setWhole ( int  whole)

Sets the whole number portion of the fraction.

Parameters:
wholeThe number to set the whole of the fraction to.

Definition at line 138 of file MFraction.cpp.


Friends And Related Function Documentation

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

Inequality operator: checks if two fractions arent equal.

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

Definition at line 278 of file MFraction.cpp.

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

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

Parameters:
leftMFraction on the left of the operator
rightMFraction on the right of the operator
Returns:
Returns the reduced product of the two fractions.

Definition at line 322 of file MFraction.cpp.

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

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

Parameters:
leftMFraction on the left of the operator
rightMFraction on the right of the operator
Returns:
Returns the reduced sum of the two fractions.

Definition at line 310 of file MFraction.cpp.

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

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

Parameters:
leftMFraction on the left of the operator
rightMFraction on the right of the operator
Returns:
Returns the reduced difference of the two fractions.

Definition at line 316 of file MFraction.cpp.

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

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

Parameters:
leftMFraction on the left of the operator
rightMFraction on the right of the operator
Returns:
Returns the reduced quotient of the two fractions.

Definition at line 328 of file MFraction.cpp.

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

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

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

Definition at line 284 of file MFraction.cpp.

ostream& operator<< ( ostream &  out,
const MFraction fraction 
) [friend]

Stream output: output fraction in the format specified by MFraction::FORMAT.

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

Definition at line 336 of file MFraction.cpp.

bool operator<= ( const MFraction left,
const MFraction 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:
leftMFraction object to the left of the operator.
rightMFraction 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 292 of file MFraction.cpp.

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

Equality operator: checks if two fractions are same.

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

Definition at line 272 of file MFraction.cpp.

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

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

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

Definition at line 298 of file MFraction.cpp.

bool operator>= ( const MFraction left,
const MFraction 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:
leftMFraction object to the left of the operator.
rightMFraction 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 304 of file MFraction.cpp.

istream& operator>> ( istream &  in,
MFraction 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 358 of file MFraction.cpp.


Member Data Documentation

C MFraction::__pad0__ [private]

= 0: output as mixed fraction when it applies, ie 5/6 and 1/2/3

Definition at line 54 of file MFraction.h.

C MFraction::DECI [private]
Initial value:
 2
                                }

= 2: output as a decimal number ie. 3.5

Reimplemented from Fraction.

Definition at line 56 of file MFraction.h.

MFraction::FracFormat MFraction::FORMAT = MIX_FRAC [static, private]

Stores how objects of type MFraction is to be outputted.

This applies to all objects of type MFraction. The default behaviour is as a mixed fraction. Use MFraction::setFormat() to set MFraction format and MFraction::getFormat() to get the format. You can set/get the following values:

Note:
Using MFraction:: instead of Fraction:: means you are working with the MFraction version of the FracFormat enumerated type. Similarly, using Fraction:: means you will be setting or gettting Fraction objects' formatting, not MFraction's.

Reimplemented from Fraction.

Definition at line 71 of file MFraction.h.

C MFraction::IM_FRAC = 1 [private]

= 1: output as improper fractions ie. 5/6, 8/5

Reimplemented from Fraction.

Definition at line 55 of file MFraction.h.

unsigned int MFraction::whole [private]

Stores the whole number portion of the fraction.

Definition at line 97 of file MFraction.h.


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