Overture  Version 25
Fraction.h
Go to the documentation of this file.
1 #ifndef FRACTION_H
2 #define FRACTION_H "fraction.h"
3 
4 #include "OvertureTypes.h"
5 #include "aString.H" // aString Class
6 class GenericDataBase;
7 
8 // Here we define a fration to be a pair of integers called
9 // numerator and denominator.
10 // Examples:
11 // (1,1)==(2,2) : "one"
12 // (1,0) : infinity
13 // (-1,0) : -infinity
14 // (1,0) <= (2,0), (5,1) <= (1,0)
15 //
16 class Fraction
17 {
18  protected:
19  int numerator;
21  public:
22  Fraction(){};
23  Fraction( const int n, const int d=1 ){ numerator=n; denominator=d; };
24  virtual ~Fraction(){}; // kkc 040415 added virtual to get rid of warnings
25 
26  virtual int get( const GenericDataBase & dir, const aString & name); // get from a database file
27  virtual int put( GenericDataBase & dir, const aString & name) const; // put to a database file
28 
29  Fraction operator + ( const Fraction & f1 ) const; // should these be return by reference?
30  Fraction operator - ( const Fraction & f1 ) const; // maybe not, see Stroustrup
31  Fraction operator - ( ) const;
32  Fraction operator * ( const Fraction & f1 ) const;
33  Fraction operator / ( const Fraction & f1 ) const;
34  int operator <= ( const Fraction & f1 ) const;
35  int operator < ( const Fraction & f1 ) const;
36  int operator >= ( const Fraction & f1 ) const;
37  int operator > ( const Fraction & f1 ) const;
38  int operator == ( const Fraction & f1 ) const;
39  int operator <= ( const real x ) const;
40  int operator < ( const real x ) const;
41  int operator >= ( const real x ) const;
42  int operator > ( const real x ) const;
43  int operator == ( const real x ) const;
44 
45 // We have to define all these member functions because we do not know
46 // how to construct a fraction from a real number!
47  friend int operator <= ( const real, const Fraction );
48  friend int operator < ( const real, const Fraction );
49  friend int operator >= ( const real, const Fraction );
50  friend int operator > ( const real, const Fraction );
51  friend int operator == ( const real, const Fraction );
52  friend real operator + ( const real, const Fraction ); // real+Fraction=real
53  friend real operator + ( const Fraction , const real );
54  friend real operator - ( const real, const Fraction ); // real-Fraction=real
55  friend real operator - ( const Fraction , const real );
56  friend real operator * ( const real, const Fraction ); // real*Fraction=real
57  friend real operator * ( const Fraction , const real );
58  friend real operator / ( const real, const Fraction ); // real/Fraction=real
59  friend real operator / ( const Fraction , const real );
60 
61  inline void setNumerator( const int n );
62  inline void setDenominator( const int d );
63  inline int getNumerator() const;
64  inline int getDenominator() const;
65 
66  friend ostream& operator<< ( ostream&, const Fraction& );
67 };
68 
69 inline void Fraction::setNumerator( const int n ){ numerator=n; }
70 inline void Fraction::setDenominator( const int d ){ denominator=d; }
71 inline int Fraction::getNumerator() const { return numerator; }
72 inline int Fraction::getDenominator() const { return denominator; }
73 
74 #endif // FRACTION_H