Create an Account

Already have account?

Forgot Your Password ?

Home / Questions / In Frac.h, replace the methodes addition( const Fraction &),subtraction(const Fraction&),.

In Frac.h, replace the methodes addition( const Fraction &),subtraction(const Fraction&),.

In Frac.h, replace the methodes addition( const Fraction &),subtraction(const Fraction&), multiply(const Fraction &);divide(const Fraction &); void printFraction(); by overloadingoperators +, -, *, /, << respectively.* Add overloading operators > and < so that they can comparetwo Fractions Write your own implementation file which is named Frac2.cpp toreplace Frac.cpp and implement the Fraction class and the thenecessary overloading operators. Download the driver Frac2Driver.cpp to test your codes. If your codes in files Frac2.h and Frac2.cppare correct, the standard output after the execution of yourprogram should like the following: 7/3 + 1/3 = 8/3 7/3 – 1/3 = 2 7/3 * 1/3 = 7/9 7/3 / 1/3 = 7 7/3 is: > 1/3 according to the overloaded > operator >= 1/3 according to the overloaded #include using namespace std; //—————————— Fraction———————————— // default constructor: parameters are numerator and denominatorrespectively // if the number is negative, the negative is always stored inthe numerator Fraction::Fraction(int n, int d) { numerator = (d < 0 ? -n : n); denominator = (d < 0 ? -d : d); reduce(); } //(a)——————————— add //————————————– // overloaded +: addition of 2 Fractions, current object andparameter Fraction Fraction::add(const Fraction a) { Fraction t; t.numerator = a.numerator * denominator + a.denominator *numerator; t.denominator = a.denominator * denominator; t.reduce(); return t; } //(b)—————————— subtract //———————————— Fraction Fraction::subtract(const Fraction a) { Fraction t; t.numerator = a.numerator * denominator – a.denominator *numerator; t.denominator = a.denominator * denominator; t.reduce(); return t; } //(c)—————————— multiply //———————————— Fraction Fraction::multiply(const Fraction a) { Fraction t; t.numerator = a.numerator * numerator; t.denominator = a.denominator * denominator; t.reduce(); return t; } //(d)——————————– divide //———————————— Fraction Fraction::divide(const Fraction a) { Fraction t; t.numerator = a.denominator * numerator; t.denominator = a.numerator * denominator; t.reduce(); return t; } //(e)—————————- printFraction //——————————— void Fraction::printFraction() { if (denominator == 1) cout << numerator << endl; else { cout << numerator << “/” << denominator<< endl; } } //(f)———————— printFractionAsFloat //—————————— void Fraction::printFractionAsFloat() { if (denominator == 0) cout << endl << “DIVIDE BY ZERO ERROR!!!” <<endl; else="" cout="" <<="" float(numerator)="" float(denominator);="" }="" ——————————–="" reduce————————————="" reduce="" fraction="" to="" lowest="" terms="" void="" fraction::reduce()="" {="" int="" n="numerator" <="" 0="" ?="" -numerator="" :="" numerator;="" d="denominator;" largest="n"> d ? n : d; int gcd = 0; // greatest common divisor for (int loop = largest; loop >= 2; loop–) if (numerator % loop == 0 && denominator % loop == 0){ gcd = loop; break; } if (gcd != 0) { numerator /= gcd; denominator /= gcd; } } ********************************************************************************************************************************************************************* // Frac.h // Proj 3 #ifndef Frac_hpp #define Frac_hpp #include using namespace std; class Fraction { public: Fraction(int = 0, int = 1); // default constructor    Fraction add(const Fraction); Fraction subtract(const Fraction); Fraction multiply(const Fraction); Fraction divide(const Fraction);    void printFractionAsFloat(); void printFraction();    private: int numerator; int denominator; void reduce(); // utility function, reduce to lowest terms }; // end class Fraction #endif /* Frac_hpp */ ********************************************************************************************************************************************************************* // driver for Fraction project part 2 #include “Frac2.h” int main() {    Fraction c( 7, 3 ), d( 3, 9 ), x; // c.printFraction();    cout << c;    cout << ” + ” ; // d.printFraction();    cout << d;    cout << ” = “;    x = c + d; // x.printFraction();    cout << x;    cout << ‘n’; // c.printFraction();    cout <<c;   ="" cout="" <<="" ”="" –="" ;="" d.printfraction();="" d;="" x="c" x.printfraction();="" <<x;="" ‘n’;="" c.printfraction();="" c;="" *="" <<c;="" x;="" is:n”;="" (="" c=""> d ) ? ” > ” : ” <= “); // d.printFraction();    cout << d;    cout << ” according to the overloaded >operatorn”;    cout << ( ( c < d ) ? ” < ” : ” >= “); // d.printFraction();    cout . . .</c;></endl;>

Apr 06 2020 View more View Less

Answer (Solved)

question Subscribe To Get Solution

Related Questions