home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX0920.H
- // Definition of Fraction class
-
- // this file is used in the following example programs:
- //--------------------------------------------------------------
- // %F,15,EX0923.CPP%EX0923.CPP overloading unary + -
- // %F,15,EX0924.CPP%EX0924.CPP overloading binary operators
- // %F,15,EX0926.CPP%EX0926.CPP overloading prefix ++ --
- // %F,15,EX0927.CPP%EX0927.CPP overloading postfix ++ --
- //---------------------------------------------------------------
- // This file must be used with
- // %F,15,EX0920.CPP%EX0920.CPP Fraction members & friends
- //---------------------------------------------------------------
-
-
- #ifndef INCL_FRAC_H
- #define INCL_FRAC_H
-
- #include <iostream.h>
-
-
- // Fractions are always kept in simplest form
- // always stored reduced to simplest form
- class Fraction
- {
-
- private:
-
- int num; // numerator signed
- int denom; // demoninator always positive
- void reduce (); // reduces Fraction to simplest form
- int // used by reduce()
- gcd( int n, int d);
-
- public:
- //------------------------------------------------------
- // default constructor
- //------------------------------------------------------
- Fraction() : num(0), denom(1) {};
-
- //------------------------------------------------------
- // constructor - conversion from 1 or 2 integers
- //------------------------------------------------------
- inline
- Fraction( int i, int j = 1);
-
- //------------------------------------------------------
- //conversion from real
- //------------------------------------------------------
- Fraction( double x);
-
- //------------------------------------------------------
- // copy constructor
- //------------------------------------------------------
- inline
- Fraction( const Fraction& source);
-
- //------------------------------------------------------
- // assignment operator
- //------------------------------------------------------
- inline Fraction&
- operator=( const Fraction& src);
-
- //------------------------------------------------------
- // overloading >> and << for input and output
- //------------------------------------------------------
- friend ostream&
- operator<<( ostream& os, const Fraction& f);
-
- friend istream&
- operator>>( istream& is, Fraction& f);
-
- //------------------------------------------------------
- // overloading unary math operators + -
- //------------------------------------------------------
- friend Fraction operator+(const Fraction& f);
-
- friend Fraction operator-(const Fraction& f);
-
- //------------------------------------------------------
- // overloading Prefix increment and decrement
- //------------------------------------------------------
- friend Fraction operator++(Fraction& f);
-
- friend Fraction operator--(Fraction& f);
-
- //------------------------------------------------------
- // overloading Postfix increment and decrement
- //------------------------------------------------------
- friend Fraction operator++(Fraction& f, int);
-
- friend Fraction operator--(Fraction& f, int);
-
- //------------------------------------------------------
- // overloading binary math operators + - * / ...
- //------------------------------------------------------
- friend Fraction
- operator+(const Fraction& f1, const Fraction& f2);
-
- friend Fraction
- operator-(const Fraction& f1, const Fraction& f2);
-
- friend Fraction
- operator*(const Fraction& f1, const Fraction& f2);
-
- friend Fraction
- operator/(const Fraction& f1, const Fraction& f2);
-
- };
-
- //----------------------------------------------------------
- // definition of inline member functions
- //----------------------------------------------------------
-
- //----------------------------------------------------------
- // constructors
- //----------------------------------------------------------
-
- inline
- Fraction::Fraction( int i, int j) : num(i), denom(j)
- { if (denom != 1) reduce();
- }
-
- inline
- Fraction::Fraction( const Fraction& source)
- { num = source.num;
- denom = source.denom;
- }
-
- //------------------------------------------------------
- // assignment operator
- //------------------------------------------------------
-
- inline Fraction&
- Fraction::operator=( const Fraction& src)
- { num = src.num;
- denom = src.denom;
- return *this;
- }
-
- #endif
-