Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Writing in C++: assume the following is already in the program: 2 mutator functi

ID: 3767712 • Letter: W

Question

Writing in C++:

assume the following is already in the program:   2 mutator functions:

-- two public Fraction mutator functions:

a. A mutator that “set”s the current numerator.

b. A mutator that “set”s the current denominator.

-- In main function, create 3 instances of the Fraction class, two Fractions to be used to perform an operation and one to hold the result.­ Create a loop and loop until the user decides to end the program. ­For each iteration of the loop, ask the user what operation he or she would like to perform or if they would like to exit the program ­ ask the user for the fraction values need to perform the selected operation ­use the Fraction mutators(given above) to update the Fraction used in the operation. Call the correct operator to perform the user requested operation ­ print the result of the operation

Explanation / Answer

Here is the code for you. If you need any further refinements, just get back to me.

#include <iostream>
using namespace std;

class Fraction
{
private:
int numerator;
int denominator;
public:
void print()
{
cout<<numerator<<" / "<<denominator;
}
Fraction operator*(Fraction second)
{
Fraction temp;
temp.setNumerator(numerator + second.getNumerator());
temp.setDenominator(denominator + second.getDenominator());
return temp;
}
Fraction operator/(Fraction second)
{
Fraction temp;
temp.setNumerator(numerator * second.getDenominator());
temp.setDenominator(denominator * second.getNumerator());
return temp;
}
Fraction operator+(Fraction second)
{
Fraction temp;
temp.setNumerator(numerator*second.getDenominator() + denominator*second.getNumerator());
temp.setDenominator(denominator * second.getDenominator());
return temp;
}
Fraction operator-(Fraction second)
{
Fraction temp;
temp.setNumerator(numerator*second.getDenominator() - denominator*second.getNumerator());
temp.setDenominator(denominator * second.getDenominator());
return temp;
}
void setNumerator(int num)
{
numerator = num;
}
void setDenominator(int den)
{
denominator = den;
}
int getNumerator()
{
return numerator;
}
int getDenominator()
{
return denominator;
}
};

int main()
{
Fraction f1, f2, f3;
int choice, numerator, denominator;
while(1)
{
cout<<"Enter the numerator of first fraction: ";
cin>>numerator;
cout<<"Enter the denominator of first fraction: ";
cin>>denominator;
f1.setNumerator(numerator);
f1.setDenominator(denominator);
cout<<"Enter the numerator of second fraction: ";
cin>>numerator;
cout<<"Enter the denominator of second fraction: ";
cin>>denominator;
f2.setNumerator(numerator);
f2.setDenominator(denominator);
cout<<"Enter the operation to perform: 1.Addition. 2. Subtraction. 3. Multiplication. 4. Division. 5. Exit."<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:   f3 = f1 + f2;   break;
case 2:   f3 = f1 - f2;   break;
case 3: f3 = f1 * f2;   break;
case 4:   f3 = f1 / f2;   break;
case 5:   return 0;
}
f3.print();
}
}