Homework: Overloaded Operators

  < Previous  Next >

For this homework assignment, submit a copy of hwkOverloadedOperators.cpp that has both the Fraction class declaration and the definition of each function necessary to compile the file. This class stores the numerator and the denominator as ints and has (some of) the corresponding operations that can be used on a fraction (e.g., adding another fraction to the current object).
For full points, do not make changes to the body of the main() function (see the Note below).
Your copy needs to compile on ranger using the following command-line:
g++  -std=c++0x  hwkOverloadedOperators.cpp
Finally, when your code is executed, the output must match the output file exactly:
Input hwkOverloadedOperators-stdin1.txt:
2
3
4
5
hwkOverloadedOperators-stdin2.txt:
9
1
8
2
hwkOverloadedOperators-stdin3.txt:
2
4
5
10
Output hwkOverloadedOperators-stdout1.txt:
aFraction: 2/3
bFraction: 3/8
2/3 + 3/8 = 25/24
3/8 - 2/3 = -7/24
++(2/3) = 5/3
(3/8)++ = 3/8
Enter numerator
Enter denominator
You entered: 2/3
Enter numerator
Enter denominator
You entered: 4/5
2/3 is smaller than 4/5
hwkOverloadedOperators-stdout2.txt:
aFraction: 2/3
bFraction: 3/8
2/3 + 3/8 = 25/24
3/8 - 2/3 = -7/24
++(2/3) = 5/3
(3/8)++ = 3/8
Enter numerator
Enter denominator
You entered: 9/1
Enter numerator
Enter denominator
You entered: 8/2
9/1 is larger than 8/2
hwkOverloadedOperators-stdout3.txt:
aFraction: 2/3
bFraction: 3/8
2/3 + 3/8 = 25/24
3/8 - 2/3 = -7/24
++(2/3) = 5/3
(3/8)++ = 3/8
Enter numerator
Enter denominator
You entered: 2/4
Enter numerator
Enter denominator
You entered: 5/10
The fractions are equal

Rubric:

Points          Item
----------      --------------------------------------------------------------
_____ /  1      Fraction class declaration
_____ /  1      operator+ and operator-
_____ /  1      operator++ (prefix)
_____ /  1      operator++ (postfix)
_____ /  1      operator==, operator< and operator>
_____ /  2      operator<<
_____ /  3      operator>>
        
_____ / 10      Total
      

Notes

  1. Start small. In the body of the main() function, temporarily comment out everything but the object instantiations and line with the addition and assignment operators. Write the necessary (single) overloaded operator. Test it. Then, and only then, move on to the next one.
  2. If you can not get an overloaded operators to work, comment it out and move on.
  3. You should verify that your output matches the above output exactly with the following commands on ranger:
    g++  -std=c++0x  hwkOverloadedOperators.cpp  -o hwkOverloadedOperators-yourlastname
    	    
    ./hwkOverloadedOperators-yourlastname  <  /nfshome/hcarroll/public_html/3110/private/hwk/hwkOverloadedOperators-stdin1.txt  &>  hwkOverloadedOperators-yourlastname1.txt
    diff  /nfshome/hcarroll/public_html/3110/private/hwk/hwkOverloadedOperators-stdout1.txt  hwkOverloadedOperators-yourlastname1.txt
    	    
    ./hwkOverloadedOperators-yourlastname  <  /nfshome/hcarroll/public_html/3110/private/hwk/hwkOverloadedOperators-stdin2.txt  &>  hwkOverloadedOperators-yourlastname2.txt
    diff  /nfshome/hcarroll/public_html/3110/private/hwk/hwkOverloadedOperators-stdout2.txt  hwkOverloadedOperators-yourlastname2.txt
    	    
    ./hwkOverloadedOperators-yourlastname  <  /nfshome/hcarroll/public_html/3110/private/hwk/hwkOverloadedOperators-stdin3.txt  &>  hwkOverloadedOperators-yourlastname3.txt
    diff  /nfshome/hcarroll/public_html/3110/private/hwk/hwkOverloadedOperators-stdout3.txt  hwkOverloadedOperators-yourlastname3.txt
    

Last Modified: