home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX10021.H < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-25  |  6.9 KB  |  230 lines

  1. // \EXAMPLES\EX10021.H
  2. //  definition of the Fraction classs
  3. //---------------------------------------------------------------
  4.  
  5. // files in this example:
  6. // EX10021.H     this file
  7. // %F,15,EX10021.CPP%EX10021.CPP   member functions of the class Fraction
  8. // %F,15,EX1002.CPP%EX1002.CPP    main program to exercise Fraction class
  9. //-------------------------------------------------------------
  10.  
  11. #ifndef EX1002_H
  12. #define EX1002_H
  13.  
  14. #include <iostream.h>
  15.  
  16. //--------------------------------------------------------
  17. // Definition of the Fraction class
  18. // Fractions are always kept in simplest form:
  19. //        num/denom  denom > 0
  20. // Fractions can be improper fractions
  21. //--------------------------------------------------------
  22.  
  23. class Fraction
  24. {
  25.  
  26. private:
  27.  
  28.    int   num;                // numerator signed
  29.    int   denom;              // demoninator always positive
  30.    void  reduce ();          // reduces Fraction to simplest form
  31.    int gcd( int n, int d);   // greatest common divisor
  32.  
  33. public:
  34.  
  35.    //------------------------------------------------------
  36.    // default constructor, convertion from integer
  37.    // creates Fraction from numerator, denominator
  38.    //------------------------------------------------------
  39.    Fraction() : num(0), denom(1) {};
  40.    inline
  41.    Fraction( int i, int j ); // default  j = 1
  42.  
  43.    //------------------------------------------------------
  44.    // conversion from real
  45.    // may reduce accuracy: minimum value 1/32737
  46.    //                    : maximun value 32767/denom
  47.    //------------------------------------------------------
  48.    Fraction( double x);
  49.  
  50.    //------------------------------------------------------
  51.    // copy constructor
  52.    //------------------------------------------------------
  53.    inline
  54.    Fraction( const Fraction& src);
  55.  
  56.    //------------------------------------------------------
  57.    // assignment operator
  58.    //------------------------------------------------------
  59.    inline Fraction&
  60.    operator=( const Fraction& src);
  61.  
  62.    //------------------------------------------------------
  63.    // conversion from Fraction to builtin types
  64.    // NOTE:  mixed mode expression would be ambiguous
  65.    //        if float() and int() are also overloaded.
  66.    //------------------------------------------------------
  67.    friend double
  68.    real(const Fraction& F);
  69.  
  70.    //------------------------------------------------------
  71.    // convert to integer - truncate
  72.    //------------------------------------------------------
  73.    friend int
  74.    trunc(const Fraction& F);
  75.  
  76.    //------------------------------------------------------
  77.    // two functions to output a Fraction
  78.    //------------------------------------------------------
  79.    ostream&
  80.    display(ostream& os);
  81.  
  82.    friend ostream&
  83.    print( const Fraction& F, ostream& os);
  84.  
  85.    //------------------------------------------------------
  86.    // invert a fraction, keep sign in numerator
  87.    //------------------------------------------------------
  88.    Fraction
  89.    Fraction::invert();
  90.  
  91.    //------------------------------------------------------
  92.    // overloading >> and <<
  93.    //------------------------------------------------------
  94.    friend ostream&
  95.    operator<<( ostream& os, const Fraction F);
  96.  
  97.    friend istream&
  98.    operator>>( istream& is, Fraction& F);
  99.  
  100.    //------------------------------------------------------
  101.    // overloading unary math operators + -
  102.    //------------------------------------------------------
  103.    inline Fraction operator+() const
  104.    { return (*this); }
  105.  
  106.    inline Fraction operator-() const
  107.    { return Fraction(-num,denom); }
  108.  
  109.    //------------------------------------------------------
  110.    // overloading Prefix increment and decrement
  111.    //------------------------------------------------------
  112.    Fraction operator++()
  113.    { num += denom;
  114.      return *this; }
  115.  
  116.    Fraction operator--()
  117.    { num -= denom;
  118.      return *this; }
  119.  
  120.    //------------------------------------------------------
  121.    // overloading Postfix increment and decrement
  122.    //------------------------------------------------------
  123.    Fraction operator++( int)
  124.    { int temp = num;
  125.      num += denom;
  126.      return Fraction(temp,denom); }
  127.  
  128.    Fraction operator--( int)
  129.    { int temp = num;
  130.      num -= denom;
  131.      return Fraction(temp,denom); }
  132.  
  133.    //------------------------------------------------------
  134.    // overloading binary math operators  +  -  *  /
  135.    // friend - first or second operand must be fraction
  136.    //------------------------------------------------------
  137.    friend Fraction
  138.    operator+( const Fraction& f1, const Fraction& f2);
  139.  
  140.    friend Fraction
  141.    operator-( const Fraction& f1, const Fraction& f2);
  142.  
  143.    friend Fraction
  144.    operator*( const Fraction& f1, const Fraction& f2);
  145.  
  146.    friend Fraction
  147.    operator/( const Fraction& f1, const Fraction& f2);
  148.  
  149.    //------------------------------------------------------
  150.    // overloading binary comparsion operators  < == >
  151.    // inline - first operand must be fraction
  152.    //------------------------------------------------------
  153.  
  154.    inline int
  155.    operator<( const Fraction& f);
  156.  
  157.    inline int
  158.    operator==( const Fraction& f);
  159.  
  160.    inline int
  161.    operator>( const Fraction& f);
  162. };
  163.  
  164.  
  165. //--------------------------------------------------------------
  166. // inline member functions
  167. //--------------------------------------------------------------
  168.  
  169. //------------------------------------------------------
  170. // default constructor, convertion from integer
  171. // creates Fraction from numerator, denominator
  172. //------------------------------------------------------
  173. inline
  174. Fraction::Fraction( int i, int j=1 ) : num(i), denom(j)
  175. { if (denom != 1 )
  176.     reduce();
  177. }
  178.  
  179. //--------------------------------------------------------------
  180. //Copy constructor
  181. //--------------------------------------------------------------
  182. inline
  183. Fraction::Fraction( const Fraction& src)
  184. {
  185.    num = src.num;
  186.    denom = src.denom;
  187. }
  188.  
  189. //------------------------------------------------------
  190. // assignment operator
  191. //------------------------------------------------------
  192. inline Fraction&
  193. Fraction::operator=( const Fraction& src)
  194. {
  195.    num = src.num;
  196.    denom = src.denom;
  197.    return *this;
  198. }
  199.  
  200. //------------------------------------------------------
  201. // binary operators
  202. //------------------------------------------------------
  203.  
  204. //------------------------------------------------------
  205. // overload operator  >  == <   comparisons
  206. //      (   operators >= != <= would be similar   )
  207. //------------------------------------------------------
  208.  
  209. inline int
  210. Fraction::operator<(const Fraction& f)
  211. {
  212.   return num * f.denom < f.num * denom;
  213. }
  214.  
  215. inline int
  216. Fraction::operator==(const Fraction& f)
  217. {
  218.   return num * f.denom == f.num * denom;
  219. }
  220.  
  221. inline int
  222. Fraction::operator>( const Fraction& f)
  223. {
  224.   return num * f.denom > f.num * denom;
  225. }
  226.  
  227. //------------------------------------------------------
  228.  
  229. #endif
  230.