Fraction Library
A C++ Fraction library.

MFraction.h

Go to the documentation of this file.
00001 
00007 #ifndef MFRACTION_H
00008         #define MFRACTION_H
00009 
00010         #include "PartialLib.h"
00011 
00012         #ifndef FRACTION_ONLY           
00013 
00014                 #include "Fraction.h"
00015 
00044                 class MFraction : public Fraction
00045                 {
00046                         public:
00047 
00051                                 enum FracFormat
00052                                 {
00053                                         MIX_FRAC = 0, 
00054                                         IM_FRAC = 1,  
00055                                         DECI = 2      
00056                                 };
00057 
00058                         private:
00059                                 //private allows only %MFraction class members access (and its friends)
00060 
00062 
00070                                 static FracFormat FORMAT;
00071 
00072                         public:
00073 
00074                                 //static functions to set and get the %MFraction format
00075                                 //static functions cant access non-static variables             
00076 
00083                                 static void setFormat(const FracFormat &format);
00084                                 
00089                                 static FracFormat getFormat();
00090 
00091                         private:
00092                 
00093                                 /* THE DATA MEMBERS: */                 
00094                                 
00096                                 unsigned int whole;
00097 
00098                                 /* HELPER METHODS PRIVATE TO CLASS */
00099 
00103                                 void reduce();
00104 
00105                         public:
00106 
00111 
00113 
00116                                 MFraction();
00117 
00122                                 MFraction(const double &number);
00123 
00131                                 MFraction(const int &numerator, const int &denominator);
00132 
00141                                 MFraction(const int &whole, const int &numerator, const int &denominator);
00142 
00150                                 MFraction(const char *frac);
00151 
00157                                 MFraction(const Fraction &frac);
00158 
00164                                 MFraction(const MFraction &frac);
00165 
00167 
00168                                 ~MFraction();
00169 
00171 
00178 
00180 
00181                                 void setWhole(int whole);
00182 
00184 
00185                                 void setNum(int numerator);
00186 
00188 
00191                                 void setDen(int denominator);
00192 
00194 
00203 
00216                                 int operator [] (const unsigned int &subscript) const;
00217 
00219 
00224                                 /*  Additionally, if values of integer, double or strings are provided, they will be automatically
00225                                         converted to %MFraction objects prior to assignment.
00226                                         
00227                                         @note This operator must be declared as a member function.
00228                                         @param right %Fraction object on the right of the equal sign. Automatic type conversions
00229                                                                  are made when values of integer, double or string is provided.                     
00230                                         @return Returns reference to object to the left of the equal sign. This makes it possible to
00231                                                         do x = y = z.
00232                                 */
00233                                 MFraction &operator = (const MFraction& right);
00234 
00241                                 MFraction operator - () const;
00242 
00249                                 MFraction operator ++ ();
00250                                 
00257                                 MFraction operator -- ();
00258                                 
00268                                 MFraction operator ++ (int inc);
00269 
00279                                 MFraction operator -- (int dec);
00280 
00282 
00290                                 MFraction operator += (const MFraction &right);
00291                                 
00303                                 MFraction operator -= (const MFraction &right);
00304 
00316                                 MFraction operator *= (const MFraction &right);
00317 
00329                                 MFraction operator /= (const MFraction &right);
00330 
00332 
00333                                 /* FRIENDS: OVERLOADED OPERATORS */
00334 
00341 
00343 
00347                                 friend bool operator == (const MFraction &left, const MFraction &right);
00348                                 
00350 
00354                                 friend bool operator != (const MFraction &left, const MFraction &right);
00355                         
00357 
00361                                 friend bool operator < (const MFraction &left, const MFraction &right);
00362                                 
00370                                 friend bool operator <= (const MFraction &left, const MFraction &right);
00371                                 
00379                                 friend bool operator > (const MFraction &left, const MFraction &right);
00380 
00388                                 friend bool operator >= (const MFraction &left, const MFraction &right);
00389 
00391 
00399 
00405                                 friend MFraction operator + (const MFraction &left, const MFraction &right);
00406                                 
00412                                 friend MFraction operator - (const MFraction &left, const MFraction &right);
00413                                 
00419                                 friend MFraction operator * (const MFraction &left, const MFraction &right);
00420                                 
00426                                 friend MFraction operator / (const MFraction &left, const MFraction &right);
00427 
00429 
00432 
00438                                 friend ostream &operator << (ostream &out, const MFraction &fraction);
00439                                 
00445                                 friend istream &operator >> (istream &in, MFraction &fraction);
00446 
00448                 };
00449         #endif /* #ifndef FRACTION_ONLY */
00450 #endif /* #ifndef MFRACTION_H */