Question
*****************C++ CODE*************C++ CODE***********C++ CODE************C++ CODE********************** ******************************************************************************************************************************** **************PLEASE CODE IN C++**************CODE USING C++*************C++************************************* Note: 1. You just need to complete the skeleton codes in two files:
*****************C++ CODE*************C++ CODE***********C++ CODE************C++ CODE**********************
********************************************************************************************************************************
**************PLEASE CODE IN C++**************CODE USING C++*************C++*************************************
Note:
1. You just need to complete the skeleton codes in two files:
1) the interface Frac.h
*************************************************************************************************
// a Fraction object holds one Fraction number, one fraction
#ifndef FRAC_H
#define FRAC_H
#include
using namespace std;
class Fraction { // not fully commented
public:
Fraction(int = 0, int = 1); // default constructor
Fraction add(const Fraction &);
/* add the prototypes of required methods here.
...
*/
void printFractionAsFloat();
private:
int numerator;
int denominator;
void reduce(); // utility function, reduce to lowest
terms
};
#endif
************************************************************************************************
2) the document and the implementation Frac.cpp
***********************************************************************************************************
// a Fraction object holds one Fraction number, one fraction
#include "Frac.h"
//------------------------------ 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
denominator = (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 -----------------------------
-------
// subtraction of 2 Fractions, current object and parameter
//(c)------------------------------ multiply -----------------------------
-------
// multiplication of 2 Fractions, current object and parameter
//(d)-------------------------------- divide -----------------------------
-------
// division of 2 Fractions, current object and parameter,
// division by zero crashes
************************************************************************************************************************************************************
2. A driver program (do not change it!) to test your class:
FracDriver.cpp
*********************************************************************************************************************
// Fraction Driver part 1
#include "Frac.h"
int main() {
Fraction x(-3,8), y(-15,-20), z, w(5);
x.printFraction();
cout
y.printFraction();
z = x.add(y);
cout
z.printFraction();
cout
z.printFraction();
cout
z.printFractionAsFloat();
cout
x.printFraction();
cout
y.printFraction();
z = x.subtract(y);
cout
z.printFraction();
cout
z.printFraction();
cout
z.printFractionAsFloat();
cout
x.printFraction();
cout
y.printFraction();
z = x.multiply(y);
cout
z.printFraction();
cout
z.printFraction();
cout
z.printFractionAsFloat();
cout
x.printFraction();
cout
y.printFraction();
z = x.divide(y);
cout
z.printFraction();
cout
z.printFraction();
cout
z.printFractionAsFloat();
cout
return 0;
}
/*Sample run:
- 3/8 + 3/4 = 3/8
3/8 = 0.375
- 3/8 - 3/4 = -9/8
- 9/8 = -1.125
- 3/8 * 3/4 = -9/32
- 9/32 = -0.28125
- 3/8 / 3/4 = -1/2
- 1/2 = -0.5
Press any key to continue . . .
*/
***************************************************************************************************************************************************************************
3. For submission, you need to upload the two source files: your Frac.h and Frac.cpp (please do NOT zip them)
Create a class called Fraction for performing arithmetic with fractions. Use integer variables to represent the private data of the class-the numerator and the denominator. Provide a constructor function that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided. Provide public member functions for each of the following: a) Addition of two Fraction numbers. See the solution as an example below. b) Subtraction of two Fraction numbers. c) Multiplication of two Fraction numbers. d) Division of two Fraction numbers e) Printing Fraction numbers in the form a/b where a is the numerator and b is the denominator f) Printing Fraction numbers in double floating-point formatStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started