Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c++ program Programming Assignment #5 Rational Class Create a class called Rational that represents a rational number. A rational number is any number that can

c++ program Programming Assignment #5 Rational Class

Create a class called Rational that represents a rational number. A rational number is any number that can be expressed as a ratio of two integers (a/b), with the denominator not equal to zero.

1) Use only two private member variables numerator and denominator. Do not add any other ones. 2) Create two constructors; one of which should be a default constructor. 3) Add all necessary Get and Set functions. 4) Create a private member helper function to store the result in the most reduced form. 4/16 must be converted to 1/4 before it is stored in the object. 5) If the denominator and the nominator are both negative numbers, the class removes the signs automatically. If the denominator is a negative number, reverse the signs. This is done by the previous private helper function. Examples: -3/-5 should be stored as 3/5. 5/-7 should be stored as -5/7. 6) Create a member function to add two Rational numbers (two objects of the class). 7) Add three more similar functions to subtract, multiply, and divide two Rational numbers (two object of the rational class). 8) Create a function to print the rational number in the form of a/b (1/4). 9) Add another function to print the number in the form of a floating point (.25). 10) Remember, all functions are member functions, such as add( ) and multiply( ). In order to test your class in your main function, prompt the user for numerator and the denominator for your first object. Repeat the prompt for the second object. Perform all mathematical operations by calling the functions. Print the two rational numbers in both print forms (a/b) and as a floating point. Then, print the result of the calculations in both forms. Make sure you label all numbers printed, for instance:

Your first rational number is: 3/2 which is 1.5. Your second number is 5/2 which is 2.5.

The sum of the numbers is 4/1 or 4.00. ... Output the rest of the result the same way.

Note that in the above example 3/2 + 5/2 = 8/2, your class stores the fraction in the reduced form, 4/1. 4.00 is the floating point version of the same number.

Allow the user to continue the process (use a loop) . Here is some code examples to get you started. Function definition: passing a Rational object to the member function of another. Rational Rational :: multiply (Rational num) { Rational temp; //creating a temporary object temp.numerator = numerator * num.numerator; temp.denominator = denominator * num.denominator; temp.simplify( ); //calling the helper function to reduce the fraction return temp; }

In your main function:

Rational num1, num2, result;

//get values from the user for num1 and num2 //some other codes result = num1.add(num2); //some other codes here result. printRational( ); result.printFloating( ); As usual, add all necessary comments. Run your program with several test cases. Capture the screen and save it. Remember, testing is as important as writing codes. In fact, the software industry spends a lot more money and time on testing and

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil

1st Edition

1558603921, 978-1558603929

More Books

Students also viewed these Databases questions

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago