Write a class to handle fractions such as “1/3”. The class should have two private data members (ints) called numerator and denominator.
Create set() and get() functions for the numerator and denominator. The numerator and denominator should be allowed to have negative values.
The value of denominator should never equal zero (a zero value for the denominator is not acceptable, as it could make the program crash). To enforce this restriction, use a while loop or an if/else statement in any function that sets the value of the denominator.
Overload the +, -, *, and / operators to perform addition, subtraction, multiplication and division of fraction objects. For example:
1/3 + 1/2 = 5/6
1/3 – 1/2 = –1/6
1/3 * 1/2 = 1/6
1/3 / 1/2 = 2/3
Also provide an overloaded method that lets your class work with cout. cout should show the fraction object. For example, this function should show 1/3 if the numerator = 1 and denominator = 3. To this effect, you could use the conversion operator const char*(), or overload the operator <<, as shown in session 13.
The operator used to work with cout should also reduce the fraction. For example, the fraction 2/6 should display as 1/3. The fraction 6/21 should display as 2/7. In order to implement this, you should use the greatest common denominator (or greatest common divisor, GCD) for the numerator and denominator. For example in the case 6/21, the GCD is 3 and 6 divided by 3 = 2, 21 divided by 3 = 7, therefore the display should be 2/7.
If the numerator or denominator (or both) are negative numbers, the operator working with cout should display the fraction correctly (if negative, the minus sign should be in front of the numerator, never in front of the denominator). For example, if the fraction data is stored as -1/-3, cout should display 1/3, if the fraction data is 1/-3, cout should display -1/3.
Write a program that demonstrates the use of each member function in the fraction class. Ask the user to enter the values for the two fractions to be used in the demonstration.
- I do not post codes before the due date, since that does not help you.
- Codes may be incorrect because I am a learner like you. Use it at your discretion.
- In this assignment, you may have a fraction class with 2 data members: numerator and denominator.
- A set method that prompts user to enter the fraction, then set the values to data members (Check if numerator or denominator equals 0).
- The requirement suggest a get() function, but I do not know what it is for.
- 4 overload operator + - * / function in which you do basic math with fraction (return type is a fraction object). You can use member or non-member overloaded function (Google)
- An overloaded cout function that reduce an input fraction and print it in correct format. Some cases need attention:
- Check if numerator or denominator equals zero
- Check if numerator equals denominator
- Check if numerator equals denominator*(-1)
- After that, reduce the function using greatest common divisor (Google)
- Check If denominator = 1
- Check If denominator = -1
- Check If numerator and denominatorhave same sign
- Check If numerator and denominatorhave opposite sign