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

This is the question I\'m working on. I have found ONE other expert answer on ch

ID: 3866686 • Letter: T

Question

This is the question I'm working on. I have found ONE other expert answer on chegg for this question, and it does not follow the instructions of the question in regards to getting user input and providing one line output for the full multiplication equation.

Design a fraction class. The class should have 2 data members to represent the numerator and denominator. Both of these numbers should obviously be integers. It should be able to display a fraction and include an operator for performing multiplication of fractions.

Construct the fraction class with 3 constructors. The first is the default. The second constructor should allow us to create a fraction that represents a whole number. The third constructor should allow us to specify the numerator and denominator of a new fraction object. The public functions should include functions for respectively getting the numerator and denominator, setting the respective numerator and denominator, multiplying fractions, and displaying the fraction. You should then use this class in a program that multiplies fractions. (Hint: you can create 3 fraction objects f1, f2, and f3. You can use the public function readFraction that reads 2 fractions f1 and f2. You can then use the statement f3 = f1.multiply(f2) to calculate f1 * f2 and assign the result to f3.)

The function's input prompts should be "Enter 1st fraction" and "Enter 2nd fraction" where the user enters the appropriate fractions. In addition, the program needs to display the result of the multiplication operation in the readable format:

3/4 * 5/6 = 15/24

This result should be displayed on one line.

Test the function with the fractions 3/4 and 5/6 and also with 1/2 and 1. (Note that your result will not provide the fraction in reduced form and you do not have to write code to reduce the fraction. Also note that we have not restricted a fraction with a denominator of 0, but this would be a nice addition to your program.)

Explanation / Answer

My g++ compiler is messed up. I have attached a code that should work.... It might throw error. But please co operate :)

#include<iostream>

using namespace std;
  


class Fraction {

private:
   int num;
   int den;
   int gcd(int a, int b) {
       int tmp;
       while (a!=b) {
           if (a > b)
               a -= b;
           else
               b -= a;
       }
       return a;
   }
   void reduce() {
       int hcf = gcd(num, den);
       num /= hcf;
       den /= hcf;
   }
public:
   Fraction(int n, int d) {
       num = n;
       den = d;
   }
  
   Fraction() {
       num = 0;
       den = 1;
   }

   void set (int n, int d) {
       num = n;
       den = d;
   }

   void subtract(Fraction a) {
       num = num*a.den - a.num*den;
       den = den * a.den;
       reduce();
   }
  
   void subtract(Fraction a, Fraction b) {
       num = a.num*b.den - b.num*a.den;
       den = a.den * b.den;
       reduce();
   }

   void multiply(Fraction a) {
       num = num*a.num;
       den = den * a.den;
       reduce();
   }
  
   void multiply(Fraction a, Fraction b) {
       num = a.num*b.num;
       den = a.den * b.den;
       reduce();
   }

   void divide(Fraction a) {
       num = num*a.den;
       den = den * a.num;
       reduce();
   }
  
   void divide(Fraction a, Fraction b) {
          num = a.num*b.den;
       den = a.den * b.num;
       reduce();
   }

    string toString() {

    return num + "/" + den;

   }


};

use toSting to fet the fraction on get the fraction in readable format!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote