Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Frac.h, replace the methodes addition( const Fraction & ),subtraction(const Fraction&), multiply(const Fraction &); divide(const Fraction &); void printFraction(); by overloading operators +, -, *,

In Frac.h, replace the methodes addition( const Fraction & ),subtraction(const Fraction&), multiply(const Fraction &); divide(const Fraction &); void printFraction(); by overloading operators +, -, *, /, << respectively.*

Add overloading operators > and < so that they can compare two Fractions

Write your own implementation file which is named Frac2.cpp to replace Frac.cpp and implement the Fraction class and the the necessary overloading operators.

Download the driver Frac2Driver.cpp

to test your codes. If your codes in files Frac2.h and Frac2.cpp are correct, the standard output after the execution of your program 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 < operator

Press any key to continue

********************************************************************************************************************************************************************

//

// Frac.h

// Proj 3

#include "Frac.h"

#include

#include

using namespace std;

//------------------------------ Fraction ------------------------------------

// default constructor: parameters are numerator and denominator respectively

// if the number is negative, the negative is always stored in the 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 and parameter

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;

int d = denominator;

int 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 << ' ';

// c.printFraction();

cout <

cout << " - " ;

// d.printFraction();

cout << d;

cout << " = ";

x = c - d;

// x.printFraction();

cout <

cout << ' ';

// c.printFraction();

cout << c;

cout << " * " ;

// d.printFraction();

cout << d;

cout << " = ";

x = c * d;

// x.printFraction();

cout <

cout << ' ';

// c.printFraction();

cout <

cout << " / " ;

// d.printFraction();

cout << d;

cout << " = ";

x = c / d;

// x.printFraction();

cout << x;

cout << ' ';

// c.printFraction();

cout << c;

cout << " is: ";

cout << ( ( c > d ) ? " > " : " <= " );

// d.printFraction();

cout << d;

cout << " according to the overloaded > operator ";

cout << ( ( c < d ) ? " < " : " >= " );

// d.printFraction();

cout << d;

cout << " according to the overloaded < operator ";

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Select Healthcare Classification Systems And Databases

Authors: Katherine S. Rowell, Ann Cutrell

1st Edition

0615909760, 978-0615909769

More Books

Students also viewed these Databases questions