Question: C++ PROGRAMMING Please provide both Fraction.cpp and Fraction.h program Create a simple Fraction class that can be used in a C++ program to perform addition,
C++ PROGRAMMING
Please provide both Fraction.cpp and Fraction.h program
Create a simple Fraction class that can be used in a C++ program to perform addition, subtraction, multiplication and division of rational numbers. Implementation You will design and implement a class Fraction that encapsulates all functions related to the processing of fractions. The << operator must be overloaded to write fractions on standard output. The class Fraction must be defined in a source file Fraction.cpp and declared in a header file Fraction.h. The implementation of the member functions should be defined in the file Fraction.cpp. The program testFraction.cpp (provided) must not be modified. It #includes the header Fraction.h and uses the Fraction class in ways that define all the member functions and operators that the Fraction class must implement. The internal representation of the fraction should be in reduced form, i.e. using a pair of numbers that have no common factors. Constructors and other member functions must ensure that the fraction is always reduced. Use Euclid's algorithm to simplify fractions to reduced form. An example of a C implementation reduce_fraction.c of this algorithm is provided in the file lecture1.zip. Do not include the file reduce_fraction.c in your submission. The operators '+', '-', '*', '/' and '=' must be overloaded and defined. Member functions getNum() and getDen() should be implemented, returning the (reduced) numerator and denominator of the fraction. Running the testFraction program must reproduce exactly the test output file testFraction.out provided (including the same amount of white space). Additional test programs will be compiled and linked to the Fraction class when grading. The testFraction program must build using the provided Makefile without generating any error messages or warnings. Error processing and special cases Any attempt to create a fraction with zero denominator should be handled by throwing an exception of type invalid_argument defined in
//
// testFraction.cpp
//
// DO NOT MODIFY THIS FILE
//
#include "Fraction.h"
#include
using namespace std;
void print_fraction(const Fraction& f)
{
cout << " print_fraction: " << f.getNum() << "/" << f.getDen() << endl;
}
int main()
{
Fraction w;
w = 4;
Fraction x(4,6);
Fraction y(5,6);
Fraction z(0);
cout << " w = " << w << endl;
cout << " x = " << x << endl;
cout << " y = " << y << endl;
cout << " z = " << z << endl;
cout << " x+y = " << x + y << endl;
cout << " x-y = " << x - y << endl;
cout << " x*y = " << x * y << endl;
cout << " x/y = " << x / y << endl;
cout << " -x = " << -x << endl;
cout << " x+2 = " << x + 2 << endl;
cout << " 2+x = " << 2 + x << endl;
cout << " x-2 = " << x - 2 << endl;
cout << " 2-x = " << 2 - x << endl;
cout << " 2*x = " << 2 * x << endl;
cout << " x*2 = " << x * 2 << endl;
cout << " x/2 = " << x / 2 << endl;
cout << " 2/x = " << 2 / x << endl;
cout << " w+x+y = " << w + x + y << endl;
print_fraction(y);
try
{
cout << " x / z = " << x / z << endl;
}
catch ( invalid_argument& e )
{
cout << "Exception: " << e.what() << endl;
}
cout << " 2 * ( x + y ) = " << 2 * ( x + y ) << endl;
Fraction u(12,18);
print_fraction(u);
}
Makefile:
# DO NOT MODIFY THIS FILE CXX=g++ CXXFLAGS=-Wall EXECS=testFraction all: $(EXECS) testFraction: testFraction.o Fraction.o $(CXX) -o $@ $^ clean: rm -f *.o $(EXECS) Output: testFraction.out
w = 4 x = 2/3 y = 5/6 z = 0 x+y = 3/2 x-y = -1/6 x*y = 5/9 x/y = 4/5 -x = -2/3 x+2 = 8/3 2+x = 8/3 x-2 = -4/3 2-x = 4/3 2*x = 4/3 x*2 = 4/3 x/2 = 1/3 2/x = 3 w+x+y = 11/2 print_fraction: 5/6 x / z = Exception: zero denominator 2 * ( x + y ) = 3 print_fraction: 2/3
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
