Question: 1410 Lab 7 Program Requirements Together, the fraction.h and fraction.cpp files form a fraction server or supplier that may be used by any client code

1410 Lab 7

Program Requirements

Together, the fraction.h and fraction.cpp files form a fraction server or supplier that may be used by any client code that needs to use fraction objects. These files illustrate class design and implementation. calc.cpp is a client that will use some but not all of the fraction class's features. The test bed will link your fraction class to a second client that will test required fraction features not used by the fraction calculator. This means that the class and the function names must be just as illustrated in the UML class diagram below - remember that C++ is case sensitive.

1410 Lab 7 Program Requirements Together, the fraction.h and fraction.cpp files form

1. Header file: name the header fraction.h

a. Create a class specification for a class named fraction (the class name begins with a lower-case 'f')

a.1. two private member variables, int numerator and int denominator

a.2 A single public constructor that serves as a default constructor (Links to an external site.), a conversion constructor (Links to an external site.) (int to fraction), and as a general constructor (Links to an external site.). You can make one constructor serve all three roles by using default arguments (Links to an external site.). As illustrated here (Links to an external site.), you should use 0 for the numerator and 1 for the denominator (why those values?). Use an initializer list (Links to an external site.) to initialize the numerator and the denominator.

a.3. The constructor must maintain the fraction in lowest terms (1/2 rather than 2/4, improper fractions like 5/3 are okay). Reduce each fraction to lowest terms in the constructor by finding the greatest common divisor (i.e., the biggest integer that divides both the numerator and the denominator evenly). If your initializer list is correct, you can use this (or similar) code as the body of your constructor:

int common = gcd(numerator, denominator); numerator /= common; denominator /= common; 

The gcd function is included at the end of the assignment.

b. Add function prototypes to the class for the functions defined in step 2 below.

2. Functions file: program functions are defined in a file named fraction.cpp

a. Define four arithmetic member functions named add, sub, mult, and div. (It's important that you use these names because I will link your fraction class to my code for testing.) Each function must accept one explicit fraction object as an argument passed by value and should return the result as a new fraction object returned by value. Do not change the values stored either of the two original fraction objects. Do not create a temporary fraction object in any of the arithmetic functions.

b. See http://www.basic-mathematics.com/basic-math-formulas.html (Links to an external site.) for the formulas for the four basic arithmetic operations on fractions. The connection between these functions and C++ objects is explained below in the section "Fraction Functions and C++"

c. Define a member function named print, which is a void function and has no explicit arguments but prints the implicit argument in the form numerator/denominator

d. Define a member function named read that reads a numerator and denominator from the keyboard into the implicit argument. The function shall

d.1 Have a return type of void

d.2 Read a single (one) fraction

d.3 Assign the parts separately: one cin >> for the numerator first followed by one cin >> for the denominator

d.4 Read two fractions by calling the read function twice

d.5Your read function is not required to reduce the faction to lowest terms (but its not hard to do if your constructor is correct)

e.5 Include the gcd function at the bottom of this file, but do NOT make it a member of the fraction class

3. Client program: the client program uses the fraction class and replaces the driver in the Time example. Create a simple fraction calculator by modifying the calc.cppa fraction server or supplier that may be used by any clientcode that needs to use fraction objects. These files illustrate class design program; change "double" to "fraction" and make any other changes needed

a.The program should loop, printing the menu below showing the four arithmetic operations and the option of exiting:

 A add S subtract M multiply D divide E exit 

a. If the user selects the exit option, the program must terminate without additional output

b. If the user selects one of the arithmetic operations, the program the prompts for two fractions (numerator first and then the denominator), carries out and displays the results of the operation. (The prompt for the numerator and denominator follows the selection of the operation.)

Euclid's Algorithm

Euclid's algorithm may be used to find the largest integer that divides two other integers (i.e., the greatest common divisor).

1. Copy and paste the "gcd" function code at the bottom of fraction.cpp

2. Add a prototype for "gcd" above the fraction class in fraction.h.

// Euclid's Algorithm for finding the greatest common divisor int gcd(int u, int v) { u = (u  0) { if (u  

Fraction Functions and C++

1. Note that within a single fraction the "/" symbol does NOT mean to divide - it separates the numerator and the denominator -- The only time "/" means to divide anything is when it is used as an operator between two fractions

2. a, b, c, and d in the formulas represent the numerators and denominators of two separate fractions in the program (but you shouldn't include variables name a, b, c, or d in your program)

2.1 a and b represent the numerator and denominator of the left hand fraction

2.2 c and d represent the numerator and denominator of the right hand fraction

3. create a new instance of the fraction class to represent the result (for addition)

3.1 the numerator is a*d + b*c

3.2 the denominator is b*d

4. the formulas are the arithmetic steps required to operate on fractions, you must translate the formulas into (a) C++ code AND (b) into objects instantiated from the fraction class. Do not create the variable a, b, c, d - that only clutters the code - instead, translate the variables into objects and member fields, e.g. : f2.numerator

5. the following picture illustrates the connection between the formulas and instances of the fraction class: f3 = f1.add(f2);

and implementation. calc.cpp is a client that will use some but not

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!