VERIFY EMAIL write a Fraction class. An example of a fraction is Note that C/C++
ID: 3817103 • Letter: V
Question
VERIFY EMAIL write a Fraction class. An example of a fraction is Note that C/C++will convert it to 0.5, but for this problem, it should still be displayed as 1/2. You should have at least the following two private member variables: numerator (op part), and denominator (bottom part). Overload the following operators. and Also, implement the default constructor and a second constructor that takes two arguments for the numerator and the denominator. Make sure the denominator is not 0. You do not have to worry about displaying the fraction to its lowest terms. For example, it is okay to display 48 However, when comparing 1/2 with 4/8, these two fractions are equal Hint 1) multiply the denominator of the first fraction with the numerator of the second fraction, 2) multiply the numerator of the first fraction with the denominator of the second fraction, 3) if the two multiplications are equal, then the two fractions are equal if you do not pass the test cases, but you know your code is c go to the next question. This could happen if you try to reduce the orrect, fraction to lowest terms. Use the following main0 function for initial testing. int main Fraction one, two; //You need to check for denominator being 0, else you will lose points cout The input format should be: NN NNIn cout Enter the first fraction In cin one; cout Enter the second fraction: In"; cin two cout "Testing The two fractions "I (one two are" are not equal n cout Testing one two one two endliExplanation / Answer
#include <iostream>
#include <stdlib.h>
using namespace std;
class Fraction
{
private:
int numerator;
int denominator;
public:
Fraction();
Fraction(int, int);
Fraction operator+(const Fraction&);
Fraction operator-(const Fraction&);
Fraction operator*(const Fraction&);
Fraction operator/(const Fraction&);
bool operator==(const Fraction& rhs);
friend ostream &operator<<( ostream &output, const Fraction & );
friend istream &operator>>( istream &input, Fraction & );
};
Fraction::Fraction() { this->denominator = 1; this->numerator = 0;}
Fraction::Fraction(int num, int denom)
{
this->numerator = num;
this->denominator = denom;
}
bool Fraction::operator==(const Fraction& rhs)
{
return (this->numerator == rhs.numerator) && (this->denominator == rhs.denominator);
}
Fraction Fraction::operator+(const Fraction& c)
{
Fraction r = Fraction();
r.numerator = (this->numerator*c.denominator + this->denominator*c.numerator);
r.denominator = (this->denominator * c.denominator);
return r;
}
Fraction Fraction::operator-(const Fraction& c)
{
Fraction r = Fraction();
r.numerator = (this->numerator*c.denominator - this->denominator*c.numerator);
r.denominator = (this->denominator * c.denominator);
return r;
}
Fraction Fraction::operator*(const Fraction& c)
{
Fraction r = Fraction();
r.numerator = (this->numerator*c.numerator);
r.denominator = (this->denominator * c.denominator);
return r;
}
Fraction Fraction::operator/(const Fraction& c)
{
Fraction r = Fraction();
r.numerator = (this->numerator*c.denominator);
r.denominator = (this->denominator * c.numerator);
return r;
}
ostream &operator<<( ostream &output, const Fraction &F )
{
output << F.numerator <<"/"<<F.denominator ;
return output;
}
istream &operator>>( istream &input, Fraction &F ) {
input >> F.numerator >> F.denominator;
if (F.denominator == 0)
{
cout << "Denominator cannot be zero. Try again." << endl;
exit(1);
}
return input;
}
void displayResult(const string &, const Fraction &, const Fraction&, const Fraction&);
int main() {
Fraction one, two, result;
int choice;
cout << "The input format should be: NN NN ";
cout << "Enter the first fraction: ";
cin >> one;
cout << "Enter the second fraction: ";
cin >> two;
cout << "Testing ==: The two fractions "
<< (one == two ? "are" : "are not") << " equal ";
cout << "Testing +: " << one << " + " << two << " = " << one + two << endl;
cout << "Testing -: " << one << " - " << two << " = " << one - two << endl;
cout << "Testing *: " << one << " * " << two << " = " << one * two << endl;
cout << "Testing /: " << one << " / " << two << " = " << one / two << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.