Has a private member function that reduces a fraction


Create a class RationalNumber (fractions) with the following functionality:
? Has a constructor that prevents a 0 denominator in a fraction and calls the reduce function to simplify the fraction
? Has a default constructor
? Has a private member function that reduces a fraction (see below)
? Has accessor and mutator functions for both numerator and denominator
? Has a function that displays the fraction in appropriate format. For example, the fraction 3/1 should be displayed as 3 and 7/2 as 3 1/2
This class should be implemented in two files, a header (.h) file and a source (.cpp) file. There should be a third file that contains a main function. In the main function, create and display at least three fractions including one that is reduced by the constructor, one with a denominator of 1 and one with a numerator greater than the denominator.


Here is the code for a function that will reduce a fraction:
// function reduction definition
void RationalNumber::reduce()
{
int largest, gcd = 1; // greatest common divisor;
largest = ( numerator > denominator ) ? numerator: denominator;
for ( int loop = 2; loop <= largest; loop++ )
if ( numerator % loop == 0 && denominator % loop == 0 )
gcd = loop;
numerator /= gcd;
denominator /= gcd;
} // end function reduction

Request for Solution File

Ask an Expert for Answer!!
Basic Computer Science: Has a private member function that reduces a fraction
Reference No:- TGS0132368

Expected delivery within 24 Hours