TextEdit File Edit Format View Window Help alld For this assignment you will be
ID: 3604072 • Letter: T
Question
TextEdit File Edit Format View Window Help alld For this assignment you will be building on your Fraction Class. However, the changes will be significant, so I would recommend your previous version as a resource when appropriate. You'l continue working on your Fraction class for o starting from scratch and usi one more week, next week. For this week you are no provide documentation and not required to simplify Fractions Please keep all of your code in one file for this week. We will separate things into three files for the next assignment Here are the client program and correct output Your class should support the following operations on Fraction objects: Construction of a Fraction from two, one, or zero integer arguments. If two arguments, they are assumed to be the numerator and denominator, Just o to be a whole number, and zero arguments creates a zero Fraction. Use defauit parameters so that you only need a single function to impleme all three of these constructors. You should check to make sure that the denominator is not set to O. The easiest way to do this is to use an assert statement: a You can put this statement at the top of your constructor. Note that the variable in the asserto) is the incoming parameter, not the data member. In ord to use assert(), you must einclude ccassert> For this assignment, you may assume that all Fractions are positive. We'll fix that next week. ssertonenominator !“ a Fraction to a stream with an overloaded cs operator. Next week we will get fancy with this, but for now just print the numerator, a forward the relational operators («,·_, >.>»,--, t-) should be supported. They should be able to compare Fractions toother Fractions as well as to integers. Either Fractions or integers can appear on either side of the binary comparison operator. You should only use one function for eadch supported. Again, they should allow Fractions to be combined with other Fractions, as well as The four basic arithmetic operations (+, /) should be ntegers. Either Fractions or integers can appear on either side of the binary operator. Only use one function for each operator Note that no special handling is needed to handle the case of dividing by a Fraction that is equal to 0. If the c is the same behavior they would expect if they tried to divide by an int or double that was equal to . The shorthand arithmetic ssserment operators(,)should aliso be implemented. Fractions can appear on the left-hand side, and Fractions ) operators should be supported in both prefix and postfix form for Fractions. To increment or decrement a Fractic integers on the right-hand side. - The increment and decrement ( means to add or subtract (respectively) one (1) acttest.cap include include catring using nanespace std; void Basietest) void Relationtest0 void ninaryhathTest)i strinm boo1string(bool convertxe) Basieteat) RelationTest ninaryMathTeat)r MathAssiqnTeat) woid Dasictest) ceut ce ITesting basio Fraction ereation&printingin; conat Fraction fr[l1- (Fraetion(4,8),Fraction(-15,2) Fraction( 10), Fraction(12, -3) Tractiont), rraction(28, 6), Fractionto, 12))Explanation / Answer
Given below is the implementaion of the Fraction class. First copy the code given by your instructor for testing the fraction class into a file say "fracttest.cpp" . Remove the 3 lines (2 #includes and using statement) given by instructor.
Instead of those lines, copy the following code . Let the rest of the code in original file from void BasicTest(); .... remain as it is.
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
class Fraction
{
private:
int numerator;
int denominator;
public:
Fraction(int num = 0, int denom = 1)
{
assert(denom != 0);
numerator = num;
denominator = denom;
}
friend ostream& operator <<(ostream &out, const Fraction& f);
bool operator <(const Fraction& f2)
{
double v1 = ((double)numerator) / denominator;
double v2 = ((double)f2.numerator) / f2.denominator;
return v1 < v2;
}
bool operator <=(const Fraction& f2)
{
double v1 = ((double)numerator) / denominator;
double v2 = ((double)f2.numerator) / f2.denominator;
return v1 <= v2;
}
bool operator >(const Fraction& f2)
{
double v1 = ((double)numerator) / denominator;
double v2 = ((double)f2.numerator )/ f2.denominator;
return v1 > v2;
}
bool operator >=(const Fraction& f2)
{
double v1 = ((double)numerator) / denominator;
double v2 = ((double)f2.numerator) / f2.denominator;
return v1 >= v2;
}
bool operator ==(const Fraction& f2)
{
double v1 = ((double)numerator) / denominator;
double v2 = ((double)f2.numerator) / f2.denominator;
return v1 == v2;
}
bool operator !=(const Fraction& f2)
{
double v1 = ((double)numerator) / denominator;
double v2 = ((double)f2.numerator) / f2.denominator;
return v1 != v2;
}
Fraction operator +(const Fraction& f2)
{
int den = denominator * f2.denominator;
int num = denominator + f2.denominator;
return Fraction(num, den);
}
Fraction operator -(const Fraction& f2)
{
int den = denominator * f2.denominator;
int num = f2.denominator - denominator;
return Fraction(num, den);
}
Fraction operator *(const Fraction& f2)
{
int den = denominator * f2.denominator;
int num = f2.denominator * denominator;
return Fraction(num, den);
}
Fraction operator /(const Fraction& f2)
{
int den = denominator * f2.numerator;
int num = f2.denominator * numerator;
return Fraction(num, den);
}
Fraction& operator +=(const Fraction& f2)
{
*this = *this + f2;
return *this;
}
Fraction& operator -=(const Fraction& f2)
{
*this = *this - f2;
return *this;
}
Fraction& operator *=(const Fraction& f2)
{
*this = (*this) * f2;
return *this;
}
Fraction& operator /=(const Fraction& f2)
{
*this = (*this) / f2;
return *this;
}
//preincrement
Fraction operator++()
{
(*this) = (*this) + 1;
return *this;
}
//postincrement
Fraction operator++(int)
{
Fraction copy = *this;
(*this)= (*this) + 1;
return copy;
}
//predecrement
Fraction operator--()
{
(*this) = (*this) - 1;
return *this;
}
//postdecrement
Fraction operator--(int)
{
Fraction copy = *this;
(*this) = (*this) - 1;
return copy;
}
};
ostream& operator <<(ostream &out, const Fraction& f)
{
out << f.numerator << "/" << f.denominator;
return out;
}
Now run the fracttest.cpp after copying the above code into it. Let me know through comments , if any issues. If the answer helped, please don't forget to rate it. Thank you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.