Question: #include using namespace std; class Ratio { private: //data members int numerator; int denominator; // Recursive helper function to return gcd of a and b

#include

using namespace std;

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<=0)

numerator = 1;

if(den<=0)

denominator=1;

int g=gcd(num,den);

numerator=num/g;

denominator=den/g;

}

//mutator function

void setNumerator(int n)

{

if(n<=0)

numerator = 1;

else

{

int g = gcd(n,getDenominator());

numerator = n / g;

}

}

//mutator function

void setDenominator(int d)

{

if(d<=0)

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 << getNumerator() << "/" << getDenominator();

}

};

//main function to test the above class

int main()

{

Ratio r;//1st object

cout<<"Ratio object created by default constructor: ";

r.print();

cout<

Ratio ra(8,12);//2nd object

cout<<"Ratio object created by two agrument constructor: ";

ra.print();

cout<

//taking user inputs

int num,den;

cout<<" Enter numerator: ";

cin>>num;

cout<<"Enter denomiator: ";

cin>>den;

ra.setDenominator(den);//modifying 2nd object

ra.setNumerator(num);//modifying 2nd object

cout<<"Ratio object after setting user inputted value: ";

ra.print();

cout<

cout<<"Double value of user inputted object: "<

return 0;

The program ontop was created as part 1 of the project which should be used for part 2 and 3

Part II: Utility Functions In c++ 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

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.

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!