Answer to Question #307320 in C++ for john

Question #307320


A function that takes two integers as inputs and sets the numerator and denominator of the fraction object. The function should check that the denominator is not zero and set the fraction to 0 over 1 if the denominator is zero.

A print function that prints a fraction object as an improper fraction, e.g. 6/4.

A print function that prints a fraction object as a mixed number, e.g. 1 2/4.

A print function that prints a fraction object as a decimal, e.g. 1.5. Each of these print functions should have a different name.

. Or you could overload the + operator to add fractions.

A subtract function that subtracts two fractions. Or overload the - operator to subtract fractions.A multiply function that multiplies two fractions. Or overload the * operator to multiply fractions.A divide function that divides two fractions. Or overload the / operator to divide fractions.To test the fraction class, create at least three different fraction objects and use these instances to test all of the above functions


1
Expert's answer
2022-03-07T10:27:04-0500
#include <iostream>
using namespace std;


class Fraction {
    int numerator;
    int denominator;


public: 
    Fraction(int num, int denom);
    void print1();
    void print2();
    void print3();
    Fraction operator+(Fraction other);
    Fraction operator-(Fraction other);
    Fraction operator*(Fraction other);
    Fraction operator/(Fraction other);
};


Fraction::Fraction(int num, int denom) {
    if (denom) {
        numerator = num;
        denominator = denom;
    }
    else {
        numerator = 0;
        denominator = 1;
    }
}


void Fraction::print1() {
    cout << numerator << "/" << denominator;
}


void Fraction::print2() {
    int i = numerator / denominator;
    int r = numerator % denominator;
    cout << i << " " << r << "/" << denominator;
}


void Fraction::print3() {
    double x = numerator / static_cast<double>(denominator);
    cout << x;
}


Fraction Fraction::operator+(Fraction other) {
    int denom = denominator * other.denominator;
    int num = numerator*other.denominator + other.numerator*denominator;
    return Fraction(num, denom);
}


Fraction Fraction::operator-(Fraction other) {
    int denom = denominator * other.denominator;
    int num = numerator*other.denominator - other.numerator*denominator;
    return Fraction(num, denom);
}


Fraction Fraction::operator*(Fraction other) {
    int denom = denominator * other.denominator;
    int num = numerator*other.numerator;
    return Fraction(num, denom);
}


Fraction Fraction::operator/(Fraction other) {
    int denom = denominator * other.numerator;
    int num = numerator*other.denominator;
    return Fraction(num, denom);
}


int main() {
    Fraction x(6, 4);
    Fraction y(2, 5);
    Fraction z(0, 0);


    cout << "Fraction x: " << endl;
    x.print1();
    cout << endl;
    x.print2();
    cout << endl;
    x.print3();
    cout << endl;


    cout << endl << "Fraction y: " << endl;
    y.print1();
    cout << endl << endl;


    z = x + y;
    cout << "x + y = ";
    z.print1();
    cout << endl;


    z = x - y;
    cout << "x - y = ";
    z.print1();
    cout << endl;


    z = x * y;
    cout << "x * y = ";
    z.print1();
    cout << endl;


    z = x / y;
    cout << "x / y = ";
    z.print1();
    cout << endl;


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog