Homework 6

15 points - Due session 7

Goal

The purpose of this assignment is to help you gain experience using reference variable types, operator overloading, and const arguments and methods.

Assignment

Part 1 - (10 points)

Consider the CFraction example we discussed in class that illustrated operator overloading. Starting from this implementation, create another method in this class called reduce() with the following signature:

CFraction reduce();

The reduce() method returns a CFraction object that represents the fully reduced form of the CFraction object on which the method was invoked. In other words, your method should create a result fraction, much like the operator*() method does. This result fraction should be initialized with the value of the current fraction (i.e. the object pointed to by this). Then, the result fraction should be reduced and returned. The current object's value should not change.

Remember, to reduce a fraction, find the largest integer that is a factor of both the numerator and the denominator, and then divide both the numerator and denominator by that number.

Next, implement comparison operators == and != for the CFraction class. Their method signatures are as follows:

bool operator==(CFraction& otherOperand);
bool operator!=(CFraction& otherOperand);

The first method returns true if the current object is equal to the second operand. The second method returns true if the current object is not equal to the second operand. Your methods must make use of the reduce() method you defined above to ensure that both operands are fully reduced. That is, you may not implement these operators by comparing the decimal equivalent of each fraction.

Finally, change the main() function to test your new methods and verify that they behave as expected.

Part 2 - (5 points)

Change the method signatures of all the overloaded operators in the CFraction class so that the fractions passed in as arguments are all references to constant objects. This is a more semantically accurate representation of the arguments, since it helps us reinforce the fact that we do not change the passed-in fractions in any way.

Next, examine all the methods in the CFraction class. Determine which ones may be declared as const methods and change those method declarations accordingly.

Deliverables

Put your three modified files into an archive file named hw5.zip or hw5.tar.Z.  Save this archive file in your AISYG 240 course directory (Courses/AISYG 240) on ftp.coe.neu.edu.