Lab 16C: Overloaded Operators

  < Previous  Next >

Assignment:

Add both the specification and the implementation for a Fraction class to the following code, lab16C.cpp, such that it compiles and produces correct output. The Fraction class needs to store a numerator and a denominator as ints and has overloaded operators such as those that are necessary in the main() function (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).
The output must match the output file exactly:
Input lab16C-stdin1.txt:
2
3
4
5
lab16C-stdin2.txt:
9
1
8
2
lab16C-stdin3.txt:
2
4
5
10
Output lab16C-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
lab16C-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
lab16C-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

Submission

Submit your source code using the handin program. For handin, for this lab, type the following in a terminal window exactly as it appears:
handin  lab16C  lab16C.cpp
To verify your submission, type the following in a terminal window:
handin  lab16C

Rubric:

Item
--------------------------------------------------------------
Fraction class declaration
operator+ and operator-
operator++ (prefix)
operator++ (postfix)
operator==, operator< and operator>
operator<<
operator>>

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 operator 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++  lab16C.cpp  -o lab16C-yourlastname
    	    
    ./lab16C-yourlastname  <  /nfshome/hcarroll/public_html/2170/private/closedLabs/lab16C-stdin1.txt  &>  lab16C-yourlastname1.txt
    diff  /nfshome/hcarroll/public_html/2170/private/closedLabs/lab16C-stdout1.txt  lab16C-yourlastname1.txt
    	    
    ./lab16C-yourlastname  <  /nfshome/hcarroll/public_html/2170/private/closedLabs/lab16C-stdin2.txt  &>  lab16C-yourlastname2.txt
    diff  /nfshome/hcarroll/public_html/2170/private/closedLabs/lab16C-stdout2.txt  lab16C-yourlastname2.txt
    	    
    ./lab16C-yourlastname  <  /nfshome/hcarroll/public_html/2170/private/closedLabs/lab16C-stdin3.txt  &>  lab16C-yourlastname3.txt
    diff  /nfshome/hcarroll/public_html/2170/private/closedLabs/lab16C-stdout3.txt  lab16C-yourlastname3.txt