Answer to Question #280178 in C++ for Faizee

Question #280178

Make a Fraction class having member data as the fraction’s numerator and denominator where both are type int. Member functions should accept input from the user in the form 3/5, and output the fraction’s value in the same format. Another member function should add two fraction values. Write a main () function that allows the user to repeatedly input two fractions and then displays their sum. After each operation, ask whether the user wants to continue.


1
Expert's answer
2021-12-16T04:46:27-0500
#include<iostream>


using namespace std;


class Fraction {
    int numerator, denominator ;
    public:
        void input();
        void addingFraction(Fraction , Fraction );
        void output();
};


void Fraction ::input(){
    char c;
	cin >> numerator >> c >> denominator;
}


void Fraction::addingFraction(Fraction f1, Fraction f2){
    numerator = f1.numerator * f2.denominator + f1.denominator * f2.numerator;
	denominator = f1.denominator * f2.denominator;
}


void Fraction::output(){
    cout << "Sum is: " << numerator << "/" << denominator << endl;
}


int main(){
    char c;
    Fraction frac1, frac2, frac3;
    do{
        cout << "Enter first fraction in p/q: ";
        frac1.input();
        cout << "Enter second fraction in p/q: ";
        frac2.input();
        frac3.addingFraction(frac1, frac2);
        frac3.output();
        cout << "Do you want to continue (y/n): ";
        cin >> c;
    }while(c == 'y' || c == 'Y');
    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