//------------------------------------------------------------------------ // fraction.h -- interface for a Fraction class // // A Fraction represents a rational number, which is not a built-in type // in C++. As we learned in gradeschool, a fraction consists of a // numerator and a denominator. We will describe fractions as [num/den]. // //------------------------------------------------------------------------ class Fraction { public: // SetValue -- set the fraction, given the numerator and denominator // PRE: denominator != 0 // POST: this fraction = [numberator/denominator] void SetValue (int numerator, int denominator); // GetValue -- get the value as a floating-point number // (may lose precision) // PRE: SetValue() has been called // FCTVAL: closest double representation of this fraction double GetValue (); // Add -- add two fractions. Returns a third, new fraction // PRE: SetValue() has been called on this instance and on rhs // FCTVAL: a fraction representing this instance plus rhs // INVAR: neither this instance nor rhs is changed Fraction Add (Fraction rhs); private: int num; // the numerator int den; // the denominator };