Question: Program Requirements Modify the fraction class created in the previous lab to appear as illustrated in the following UML class diagram. The UML does not

Program Requirements

Modify the fraction class created in the previous lab to appear as illustrated in the following UML class diagram. The UML does not have a notation to denote "friend" functions, but all of the arithmetic functions must be friends.

Program Requirements Modify the fraction class created in the previous lab to

Convert the four arithmetic fraction and the two I/O functions to overloaded operators. This means to replace the arithmetic and I/O functions with overloaded operators that define the symbols +, -, *, /, > so that they work for instances of the fraction class:

add ? operator+

sub ? operator-

mult ? operator*

div ? operator/

print ? operator

read ? operator>>

When you add operator> to the fraction class, you will be using the class names "ostream" and "istream" in the fraction header file. This means that you must also #include and add the "using namespace std;" statement at the top of fraction.h

Modify calc.cpp to use the overloaded operators in place of the functions written in the last lab. That is, replace the function calls with operators as listed in 1.a through 1.f

Implement each overloaded arithmetic operator as as single (i.e., just one) binary friend function (Links to an external site.)Links to an external site.

All four arithmetic operators must support both fraction and integer operands as demonstrated by the three statements below where f1 and f2 are instances of the fraction class and i is an int (your calc program will not use all three versions but I will link your fraction code to a program that will test all three):

fraction f3 = f1 + f2;

fraction f4 = f2 + 2;

fraction f5 = 2 + f1;

Note that you will not write this code in calc.cpp or any of your faction files. These statements merely demonstrate what your fraction functions must be capable of doing.

Here is the code I have so far:

calc.cpp

#include "fraction.h"

#include

using namespace std;

int main() {

char choice;

fraction left;

fraction right;

fraction result;

do {

cout

cout

cout

cout

cout

cout

cin >> choice;

cin.ignore();

switch (choice) {

case 'A':case 'a':

left.read();

right.read();

result = left.add(right);

result.print();

break;

case 'S':case 's':

left.read();

right.read();

result = left.sub(right);

result.print();

break;

case 'M':case 'm':

left.read();

right.read();

result = left.mult(right);

result.print();

break;

case 'D':case 'd':

left.read();

right.read();

result = left.div(right);

result.print();

break;

case 'e':case 'E':

break;

default:

cerr

break;

}

} while (choice != 'e' && choice != 'E');

return 0;

}

fraction.cpp

#include

#include

#include "fraction.h"

using namespace std;

fraction fraction::add(fraction f){

int n = (numerator * f.denominator) + (f.numerator * denominator);

int d = denominator * f.denominator;

return fraction(n, d);

}

fraction fraction::sub(fraction f){

int n = (numerator * f.denominator) - (f.numerator * denominator);

int d = denominator * f.denominator;

return fraction(n, d);

}

fraction fraction::mult(fraction f){

int n = numerator * f.denominator;

int d = denominator * f.denominator;

return fraction(n, d);

}

fraction fraction::div(fraction f){

int n = numerator * f.denominator;

int d = denominator * f.numerator;

return fraction(n, d);

}

void fraction::print()

{

cout

}

void fraction::read()

{

cout

cin >> numerator;

cout

cin >> denominator;

}

int gcd(int u, int v) {

u = (u

v = (v

while (u > 0) {

if (u

int t = u; // swap u and v

u = v;

v = t;

}

u -= v;

}

return v; // the GCD of u and v

}

fraction.h

#pragma once

#include

using namespace std;

int gcd(int n, int d);

class fraction {

private:

int numerator;

int denominator;

public:

fraction(int n = 0, int d = 1) : numerator(n), denominator(d) {

int common = gcd(numerator, denominator);

numerator /= common;

denominator /= common;

}

fraction add(fraction f);

fraction sub(fraction f);

fraction mult(fraction f);

fraction div(fraction f);

void print();

void read();

};

fraction numerator: int denominator: int +fraction(n: int 0, d: int1) toperator+(f1 fraction, f2:fraction): fraction +operator-(f1 fraction, f2: fraction): fraction +ope rator*(f1 fraction, f2 fraction) : fraction +operator/(f1: fraction, f2: fraction): fraction +operator>(in: istream&, f: fraction&) istre am &

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!