Question: I have an assignment that I need your help with. This is on Ratio Data Type. Use C++ (Dev-C++ preferred so I can see the
I have an assignment that I need your help with. This is on Ratio Data Type. Use C++ (Dev-C++ preferred so I can see the output). Part I was done last week so I will post the program here. The assignment now is to do Part II and Part III. Thanks in advance. 


And here is the program for Part I incase, posted here.
#include
class Ratio
{
private:
//data members
int numerator;
int denominator;
// Recursive helper function to return gcd of a and b
int gcd(int a, int b)
{
// base case
if (a == b)
return a;
//if a is greater
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
public:
//default constructor
Ratio()
{
numerator = 1;
denominator = 1;
}
//two argument constructor
Ratio(int num,int den)
{
if(num
numerator = 1;
if(den
denominator=1;
int g=gcd(num,den);
numerator=num/g;
denominator=den/g;
}
//mutator function
void setNumerator(int n)
{
if(n
numerator = 1;
else
{
int g = gcd(n,getDenominator());
numerator = n / g;
}
}
//mutator function
void setDenominator(int d)
{
if(d
denominator = 1;
else
{
int g = gcd(getNumerator(),d);
denominator = d / g;
}
}
//accessor gunction
int getNumerator()
{
return numerator;
}
//accessor function
int getDenominator()
{
return denominator;
}
//get double value
double getDoubleValue()
{
double value=(double)getNumerator()/getDenominator();
return value;
}
//helper function
void print()
{
cout
}
};
//main function to test the above class
int main()
{
Ratio r;//1st object
cout
r.print();
cout
Ratio ra(8,12);//2nd object
cout
ra.print();
cout
//taking user inputs
int num,den;
cout
cin>>num;
cout
cin>>den;
ra.setDenominator(den);//modifying 2nd object
ra.setNumerator(num);//modifying 2nd object
cout
ra.print();
cout
cout
return 0;
}
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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
