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

  1. // \EXAMPLES\EX0920.H
  2. //  Definition of Fraction class
  3.  
  4. //  this file is used in the following example programs:
  5. //--------------------------------------------------------------
  6. // %F,15,EX0923.CPP%EX0923.CPP    overloading unary + -
  7. // %F,15,EX0924.CPP%EX0924.CPP    overloading binary operators
  8. // %F,15,EX0926.CPP%EX0926.CPP    overloading prefix ++ --
  9. // %F,15,EX0927.CPP%EX0927.CPP    overloading postfix ++ --
  10. //---------------------------------------------------------------
  11. // This file must be used with
  12. // %F,15,EX0920.CPP%EX0920.CPP    Fraction members & friends
  13. //---------------------------------------------------------------
  14.  
  15.  
  16. #ifndef INCL_FRAC_H
  17. #define INCL_FRAC_H
  18.  
  19. #include <iostream.h>
  20.  
  21.  
  22.  // Fractions are always kept in simplest form
  23.  // always stored reduced to simplest form
  24. class Fraction
  25. {
  26.  
  27. private:
  28.  
  29.    int   num;        // numerator signed
  30.    int   denom;      // demoninator always positive
  31.    void  reduce ();  // reduces Fraction to simplest form
  32.    int               // used by reduce()
  33.    gcd( int n, int d);
  34.  
  35. public:
  36.    //------------------------------------------------------
  37.    // default constructor
  38.    //------------------------------------------------------
  39.    Fraction() : num(0), denom(1) {};
  40.  
  41.    //------------------------------------------------------
  42.    // constructor - conversion from 1 or 2 integers
  43.    //------------------------------------------------------
  44.    inline
  45.    Fraction( int i, int j = 1);
  46.  
  47.    //------------------------------------------------------
  48.    //conversion from real
  49.    //------------------------------------------------------
  50.    Fraction( double x);
  51.  
  52.    //------------------------------------------------------
  53.    // copy constructor
  54.    //------------------------------------------------------
  55.    inline
  56.    Fraction( const Fraction& source);
  57.  
  58.    //------------------------------------------------------
  59.    // assignment operator
  60.    //------------------------------------------------------
  61.    inline Fraction&
  62.    operator=( const Fraction& src);
  63.  
  64.    //------------------------------------------------------
  65.    // overloading >> and <<  for input and output
  66.    //------------------------------------------------------
  67.    friend ostream&
  68.    operator<<( ostream& os, const Fraction& f);
  69.  
  70.    friend istream&
  71.    operator>>( istream& is, Fraction& f);
  72.  
  73.    //------------------------------------------------------
  74.    // overloading unary math operators + -
  75.    //------------------------------------------------------
  76.    friend Fraction operator+(const Fraction& f);
  77.  
  78.    friend Fraction operator-(const Fraction& f);
  79.  
  80.    //------------------------------------------------------
  81.    // overloading Prefix increment and decrement
  82.    //------------------------------------------------------
  83.    friend Fraction operator++(Fraction& f);
  84.  
  85.    friend Fraction operator--(Fraction& f);
  86.  
  87.    //------------------------------------------------------
  88.    // overloading Postfix increment and decrement
  89.    //------------------------------------------------------
  90.    friend Fraction operator++(Fraction& f, int);
  91.  
  92.    friend Fraction operator--(Fraction& f, int);
  93.  
  94.    //------------------------------------------------------
  95.    // overloading binary math operators  +  -  *  /   ...
  96.    //------------------------------------------------------
  97.    friend Fraction
  98.    operator+(const Fraction& f1, const Fraction& f2);
  99.  
  100.    friend Fraction
  101.    operator-(const Fraction& f1, const Fraction& f2);
  102.  
  103.    friend Fraction
  104.    operator*(const Fraction& f1, const Fraction& f2);
  105.  
  106.    friend Fraction
  107.    operator/(const Fraction& f1, const Fraction& f2);
  108.  
  109. };
  110.  
  111. //----------------------------------------------------------
  112. // definition of inline member functions
  113. //----------------------------------------------------------
  114.  
  115. //----------------------------------------------------------
  116. // constructors
  117. //----------------------------------------------------------
  118.  
  119. inline
  120. Fraction::Fraction( int i, int j) : num(i), denom(j)
  121. {  if (denom != 1) reduce();
  122. }
  123.  
  124. inline
  125. Fraction::Fraction( const Fraction& source)
  126. {  num = source.num;
  127.    denom = source.denom;
  128. }
  129.  
  130. //------------------------------------------------------
  131. // assignment operator
  132. //------------------------------------------------------
  133.  
  134. inline Fraction&
  135. Fraction::operator=( const Fraction& src)
  136. {  num = src.num;
  137.    denom = src.denom;
  138.    return *this;
  139. }
  140.  
  141. #endif
  142.