// Fraction.H - A Fraction class in C++ // // Author: James Kukla // Date: 21 Feb 2000 // Last Modified: 21 Feb 2000 // // Description: // ----------- // This class should serve as an example of how to go about // writing your own classes in C++. Up until now, much of the course // material has been devoted to a brief overview of the features of C++ // and not specifically on how to write code in an object-oriented style // in C++. The first step to covering that material is defining a new // object type. // // This is a simple (and incomplete) Fraction class in C++. It // should help to illustrate two points: // // (1) the organization of "modules" (pairs of .C and .H files that // define objects) // (2) how to reuse your own code to reduce programming errors // and increase your productivity. #ifndef CS202_FRACTION_H #define CS202_FRACTION_H class Fraction { public: // ----------------------------------------------------------------- // Purpose: Fraction() is used for creating "empty" // Fractions. // Parameters: None // Returns: Nothing // Preconditions: None // Postconditions:The newly created Fraction is set to 0/1. // ----------------------------------------------------------------- Fraction(); // ----------------------------------------------------------------- // Purpose: Fraction(int) is used for initializing // Fractions for whole numbers (i.e. the // denominator is 1). // Parameters: n - The numerator for the new fraction // Returns: Nothing // Preconditions: None // Postconditions:The newly created Fraction is set to n/1. // ----------------------------------------------------------------- Fraction(int n); // ----------------------------------------------------------------- // Purpose: Fraction(int, int) is used for initializing // Fractions with an explicit numerator and // denominator. // Parameters: n - The numerator for the new Fraction // d - The denominator for the new Fraction // Returns: Nothing // Preconditions: None // Postconditions:The newly created Fraction is set to n/d.. // ----------------------------------------------------------------- Fraction(int n, int d); // ----------------------------------------------------------------- // Purpose: operator +(Fraction) is a binary operator // that produces the sum of *this and rhs. // Parameters: rhs - The right-hand operand to the +. // Returns: A new Fraction that is the sum of the // *this and rhs. // Preconditions: None // Postconditions:None // ----------------------------------------------------------------- Fraction operator +(Fraction rhs); // ----------------------------------------------------------------- // Purpose: operator -(Fraction) is a binary operator // that produces the difference of *this and rhs. // Parameters: rhs - The right-hand operand to the -. // Returns: A new Fraction that is the difference of the // *this and rhs. // Preconditions: None // Postconditions:None: // ----------------------------------------------------------------- Fraction operator -(Fraction rhs); // ----------------------------------------------------------------- // Purpose: operator -() is a unary minus operator for // Fractions. // Parameters: None // Returns: A new Fraction that is -n/d. // Preconditions: None // Postconditions:None // ----------------------------------------------------------------- Fraction operator -(); // ----------------------------------------------------------------- // Purpose: operator <(Fraction) evaluates to nonzero if // the lefthand argument is < the righthand // argument and zero otherwise. // Parameters: rhs - The righthand argument of <. // Returns: Zero if the lhs is NOT < the rhs // Nonzero otherwise // Preconditions: None // Postconditions:None // ----------------------------------------------------------------- int operator <(Fraction rhs); // ----------------------------------------------------------------- // Purpose: operator ==(Fraction) evaluates to nonzero if // the lefthand argument is == the righthand // argument and zero otherwise. // Parameters: rhs - The righthand argument of <. // Returns: Zero if the lhs is NOT == the rhs // Nonzero otherwise // Preconditions: None // Postconditions:None // ----------------------------------------------------------------- int operator ==(Fraction rhs); // ----------------------------------------------------------------- // Purpose: operator <=(Fraction) evaluates to nonzero if // the lefthand argument is <= the righthand // argument and zero otherwise. // Parameters: rhs - The righthand argument of <=. // Returns: Zero if the lhs is NOT <= the rhs // Nonzero otherwise // Preconditions: None // Postconditions:None // ----------------------------------------------------------------- int operator <=(Fraction rhs); // ----------------------------------------------------------------- // Purpose: Accessor method to return the numerator of // the current Fraction. // Parameters: None // Returns: The value of numerator. // Preconditions: None // Postconditions:None // ----------------------------------------------------------------- int getNumerator(); // ----------------------------------------------------------------- // Purpose: Accessor method to return the denominator of // the current Fraction. // Parameters: None // Returns: The value of denominator. // Preconditions: None // Postconditions:None // ----------------------------------------------------------------- int getDenominator(); private: int numerator, denominator; }; // ----------------------------------------------------------------- // Purpose: Overloaded ouput operator for printing Fractions. // Parameters: output - The ostream & that we're printing to // f - A reference to the Fraction we want to print. // Returns: The ostream & output AFTER we've written // f to it. // Preconditions: output must be a valid ostream // Postconditions: None // ----------------------------------------------------------------- ostream & operator <<(ostream & output, Fraction & f); #endif