Question: Assignment 6 - Fraction Direction: Submit the typed source codes or git url. Building the Class For this assignment, you will create a fraction class
Assignment 6 - Fraction Direction: Submit the typed source codes or git url.
Building the Class For this assignment, you will create a fraction class that can perform every fracion arithmetic operation. The class Fraction should be stored in the header file Fraction.h and should consist of: Private int field named numerator. Private int field named denominator. Public default constructor that assigns 1 to both numerator and denominator.
Public overloaded constructor that takes an int as a parameter named numerator. It assigns numerator to the numer- ator field and 1 to the denominator field.
Public overloaded constructor that takes two ints as a parameter named numerator and denominator respectively. If denomintor equals 0 throw the exception Error: Cannot divide by zero; otherwise, it should assign numerator to the numerator field and denominator to the denominator field. Furthermore, if both fields are negative, make them both positive. And if the denominator field is negative, make it positive and negate (multiply it by negative one) the numerator field. Public copy constructor that performs a shallow copy. Public overloaded assignment operator that performs a shallow copy. Public constant get method for numerator. Public constant get method for denominator. Public set method for numerator. Public set method for denominator. If the parameter is 0 throw the exception Error: Cannot divide by zero. And if the parameter is negative, assign denominator the absolute value of the parameter and negate the numerator field. Public double constant method named Decimal() that takes no parameters. It should return the fraction in decimal form. Friend Fraction reference function named Sum() that takes two constant Fraction references as parameters. It returns the sum of the parameters. The denominator of the sum should be the least common multiple of the denuminator s of the Fraction objects. Friend Fraction reference function named Difference() that takes two constant Fraction references as parameters. It returns the difference of the parameters. The denominator of the defference should be the least common multiple of the denuminator s of the Fraction objects. Friend Fraction reference function named Product() that takes two constant Fraction references as parameters. It returns the product of the parameters. Friend Complex reference function named Quotient() that takes two constant Fraction references as parameters. It returns the quotient of the parameters only if the divisor parameter is not 0; otherwise, it throws the exception Error: Cannot divide by zero.. Public string constant method named ToString() that takes no parameters. It should return a string in the format numerator followed by denominator with a forward slash (/) between them. Friend overloaded ostream operator. It displays a Fraction object in the same format as ToString(). Implementing the Class In the main function, create at least three Fraction objects with distinct values. display each fraction and their decimal forms. display two distinct sums. display two distinct differences. display two distinct products.
display two distinct quotients. The following is an example of a possible output: (-3/2,-1.5) (7/4,1.75) (0/6,0) -3/2 + 7/4 = 1/4 7/4 + 0/6 = 21/12 (0/6 - -3/2 = 9/6 -3/2 - -3/2 = 0/2 0/6 * 7/4 = 0/12 -3/2 * -3/2 = 9/4 -3/2 / 7/4 = -12/14 7/4 / -3/2 = -14/12
Extra Credit Modify the necessary constructors and the set method for numerator in the Fraction class to set denominator equal to 1 if numerator equals 0. Extra Credit Do Extra Credit. The friend Fraction reference function named Simplify() that takes a constant Fraction reference as a parameters. It returns the simplified equivalent fraction of the parameter (that is, if the parameter was 5/10, the function would return 1/2). Modify the main to have the display of the results of the operations simplified.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
