## Need Help Creating a Fraction CLass

Instructions:
Write a class to handle objects of fraction type. In its simplest form, a fraction is just two integer values: a numerator and a denominator. fractions can be negative, may be improper (larger numerator than denominator), and can be whole numbers (denominator of 1).

You should write the class in three phases. I've set it up this way because there are a couple of functions that are very difficult and I want to provide a way for you to skip those two functions and move on if you need to. If you submit the assignment with only phase 1 (or phases 1 and 2) completed you can still earn almost all of the points.

• Phase 1 Write everything except the extraction operator (>>) and the reduce function. To do this, you'll want to comment out that part of the client program that tests the extraction operator, and just leave fractions un-reduced.
Don't try to write phase 1 all at once! Use iterative development! By one week before the due date you should have completed and exhaustively tested the following: the constructors, a super-simple version of the insertion operator (just print the numerator, a slash, and the denominator, so you can see if your output is correct), the basic arithmetic operators, and the relational operators. You can test these by commenting out irrelevant portions of the provided client.

Your class should support the following operations on fraction objects:

• Construction of a fraction from two, one, or zero integer arguments. If two arguments, they are assumed to be the numerator and denominator, just one is assumed to be a whole number, and zero arguments creates a zero fraction.
• Printing a fraction to a stream with an overloaded << operator. The fraction should be printed in reduced form (not 3/6 but 1/2). Whole numbers should print without a denominator (e.g. not 3/1 but just 3). Improper fractions should be printed as a mixed number with a + sign between the two parts (2+1/2). Negative fractions should be printed with a leading minus sign.
• Reading a fraction from a stream using an overloaded >> operator. You should be able to read any of the formats described above (mixed number, negative, whole numbers). You may assume that there are no spaces or formatting errors in the fractions that you read. Note: You may need to exceed 15 lines for this function. My solution is about 20 lines long.
• All six of the relational operators (<, <=, >, >=, ==, !=) should be supported. They should be able to compare fractions to other fractions as well as fractions to integers. Either fractions or integers can appear on either side of the binary comparison operator.
• The four basic arithmetic operations (+, -, *, /) should be supported. Again, they should allow fractions to be combined with other fractions, as well as with integers. Either fractions or integers can appear on either side of the binary operator.
• The shorthand arithmetic assignment operators (+=, -=, *=, /=) should also be implemented. fractions can appear on the left-hand side, and fractions or integers on the right-hand side.
• The increment and decrement (++, --) operators should be supported in both prefix and postfix form for fractions. To increment or decrement a fraction means to add or subtract (respectively) one (1).

• The name of your class must be "fraction". No variations will work.
• Use exactly two data members.
• Your class must guarantee that fractions are stored in reduced form at all times, not just when they are output.
• The '+' in mixed numbers does not mean add. It is simply a separator (to separate the integer part from the fraction part of the number). So the fraction "negative two and one-sixth" would be written as -2+1/6, even though -2 plus 1/6 is not what we mean.
• Your extraction operator must be able to handle a plain integer in the input stream (and store it as a fraction). Since your extraction operator should not consume anything after the end of the fraction being read, you will have to use the .peek() function to look ahead in the input stream and see what the next character is after the first number is read. If it's not either a '/'' or a '+', then you are done reading and should read no further. I have something like this:
in >> temp;
if (in.peek() == '+'){
doSomething...
} else if (in.peek() == '/'){
doSomethingElse...
} else {
doThirdOption
}
• You should not compare two fractions by dividing the numerator by the denominator. This is not guaranteed to give you the correct result every time. I would simply cross multiply and compare the products.
• Don't go to a lot of trouble to find the common denominator (when adding or subtracting). Simply multiply the denominators together.
• The last two bullets bring up an interesting issue: if your denominators are really big, multiplying them together (or cross multiplying) may give you a number that is too big to store in an int variable. This is called overflow. The rule for this assignment is: don't worry about overflow in these two situations. If you have a problem with overflow even when you are using denominators that are small enough to be multiplied together, then you have a problem.
• About comments: Don't forget to read the Style Conventions section of our syllabus, where commenting guidelines for classes are described in detail. In particular, every public member function (and friend function), however simple, must have a precondition (if there is one) and a postcondition listed. Only the most complex of your function definitions will need additional comment.
• My solution had 22 functions. All but three of them (<<, >>, and reduce()) were less than 4 lines long. I'm not saying yours has to be like this, but it shouldn't be way off. My reduce() and << have about 10 - 14 lines. >> has 21 lines. This is including declarations and lines that have nothing but a close curly brace.
I have included the files below on what I have so far.
client.txtclassprogram.txt