home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK4 / SAMPLES / CPPTUTOR / FRAC2.H$ / FRAC2
Encoding:
Text File  |  1991-12-11  |  1.2 KB  |  38 lines

  1. // FRAC2.H
  2.  
  3. // This is an example class from Chapter 8 of the C++ Tutorial. This
  4. //     class demonstrates the overloaded binary + operator using three
  5. //     separate functions, the overloaded unary - operator, and two
  6. //     user-defined conversions: one using a conversion operator, and
  7. //     one using a single-argument constructor. This class avoids the
  8. //     ambiguity errors that FRAC1.H causes for statements like
  9. //     a = b + 1234 (where a and b are Fraction objects). The use
  10. //     of three separate operator+ functions resolves the ambiguity.
  11.  
  12. #if !defined( _FRAC2_H_ )
  13.  
  14. #define _FRAC2_H_
  15.  
  16. class Fraction
  17. {
  18. public:
  19.     Fraction();
  20.     Fraction( long num, long den = 1 );
  21.     void display() const;
  22.     operator float() const;
  23.     friend Fraction operator+( const Fraction &first,
  24.                                const Fraction &second );
  25.     friend Fraction operator+( long first,
  26.                                const Fraction &second );
  27.     friend Fraction operator+( const Fraction &first,
  28.                                long second );
  29.     friend Fraction operator-( const Fraction &one );
  30. private:
  31.     static long gcf( long first, long second );
  32.     long numerator,
  33.          denominator;
  34. };
  35.  
  36. #endif // _FRAC2_H_
  37.  
  38.