Question: C++ Part I: Class Structure Create a class called Ratio for performing arithmetic with fractions. Write a program to test your class. Use integer variables

C++

Part I: Class Structure Create a class called Ratio for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the instance variables - the numerator and the denominator. The data members should be private. Provide appropriate accessor and mutator functions. Provide a constructor that enables an object of this class to be initialized when it is declared. Provide a no-argument constructor with default values of 1 in case no initial values are provided. getDoubleValue is a public function that returns a double with the same value as the underlying Ratio number. This function should not accept any parameters. Store the numerator and denominator as separate data members of the class. The numerator and denominator should be stored so that the ratio is always in reduced form. Fractions are said to be in reduced form if the greatest common divisor of the numerator and the denominator is 1. (For example, 3/6 is equivalent to 1/2 and would be stored by the class as 1 in the numerator and 2 in the denominator.) It will be necessary to reduce the ratio when an object is created (from the constructor) and whenever the numerator or denominator change in value (from the mutator functions). It is probably most efficient to make a function whose purpose is to reduce the current ratio by considering the numerator and denominator instance variables. Then the reduce function can be performed whenever its needed. Part II: Utility Functions Create separate functions (outside of the class) to perform the following operations: void printRatio(Ratio r): Print a Ratio in the form a/b. Ratio getRatio(): Get input from the user for a numerator and denominator and return a Ratio with the values indicated by the user. Provide public functions that perform arithmetic on your new data type: Add, Subtract, Multiply, Divide two Ratios. The result of arithmetic operations should be a Ratio in reduced form. Review the rules of fractional arithmetic to make sure your functions work as one would expect them to. Remember that for addition and subtraction you need to find a common denominator, while that is not necessary for multiplication and division. Here is a sample main function: int main() { Ratio frac1; frac1.setNumerator(2); frac1.setDenominator(7); Ratio frac2(3, 10); cout<< frac1.getDoubleValue() //prints .285714 Ratio sum = addRat(frac1, frac2); //stores 41/70 Ratio div = subtractRat(frac1, frac2); //stores -1/70 Ratio product = multiplyRat(frac1, frac2); //stores 3/35 (which is the reduced form of 6/70) printRatio(product); //prints 3/35 Ratio quotient = divideRat(frac1, frac2); //stores 20/21 } Part III: Separate compilation Split your program into 2 files: Ratio.h and client.cpp. Ratio.h contains the class declaration and definitions of Ratio member functions. client.cpp contains the main program. Bonus: Overload the +, -, *, / operators to perform arithmetic operations on your new Ratio data type. Then the following main function should be valid. int main() { Ratio frac1(3, 10); Ratio frac2(3, 10); Ratio sum = frac1 + frac2; //stores 41/70 Ratio div = frac1 - frac2; //stores -1/70 Ratio product = frac1 * frac2; //stores 3/35 (which is the reduced form of 6/70) printRatio(product); //prints 3/35 Ratio quotient = frac1 / frac2; //stores 20/21 }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!