In this lab, we will implement create a Fraction class, complete with operator overloading and overloaded input and output operators. Here is the header file:
#ifndef _FRACTION_H_
#define _FRACTION_H_
class Fraction
{
friend ostream &operator<<( ostream&, const Fraction&);
friend istream &operator>>( istream&, Fraction&);
public:
//ctors
Fraction (int num = 0, int dem = 1);
Fraction (const Fraction& other);
//Assignment
const Fraction& operator=(const Fraction& rhs);
const Fraction& operator+=(const Fraction& rhs);
//Arithmetic
Fraction operator+(const Fraction& rhs) const;
Fraction operator-(const Fraction& rhs) const;
Fraction operator*(const Fraction& rhs) const;
//Other functions
void print (ostream & out = cout) const;
bool equals(const Fraction& rhs) const;
private:
int numerator;
int denominator;
};
#endif
Your job is to implement all of these methods and turn this into a working class. Don't get scared! It's not as hard as it looks, and the book provides some really good examples.
Hints:
- Don't worry about simplifying fractions. If you multiply 2/5 * 5/5, having 10/25 (as opposed to 2/5) as your answer is acceptable.
What to hand in:
- Both the .h and the .cc file, as well as a test program you create that shows off all this functionality.