Following the instruction below and write the program GIVEN
Following the instruction below and write the program. GIVEN: MAIN. SHOW THE OUTPUT!!!!!!
Create a class for working with fractions. Only 3 private data members are needed: the int whole number part of the fraction, the int numerator of the fraction, and the positive int denominator of the fraction. For example, the fraction 3 5/8 will have the three private data member values of 3, 5, and 8. The following methods should be in your class:
a. A default constructor that should use default arguments in case no initializers are included in the main. The fraction needs to be stored in reduced form. Make sure the denominator is not set to 0 or a negative value.
b. Add two mixed fractions and store the sum in reduced form.
c. Subtract two mixed fractions and store the difference in reduced form.
d. Multiply two mixed fractions and store the product in reduced form.
e. Divide two mixed fractions and store the quotient in reduced form.
f. Print a mixed fraction.
g. Change a fraction to its reciprocal. For example, the reciprocal of the fraction 3 5/8 is 8/29. The reciprocal of 4/19 is 4 3/4
Your main should instantiate two mixed fractions and call each of the class methods. The two fractions should be printed a long with the sum, difference, product, quotient, and after being changed to its reciprocal.
Write the program where the following operators are overloaded:
>> << + - * / == !=
Also overload the preincrement and post increment operators ++ which will change a fraction to its reciprocal.
int main() // make sure to simplify/reduce in operator >>
{
Fraction f1, f2, sum, diff, prod, quo;
cin >> f1;
cin >> f2;
cout << \"first fraction is \" << f1 << endl << endl;
cout << \"second fraction is \" << f2 << endl << endl;
sum = f1 + f2;
diff = f1 - f2;
prod = f1 * f2;
quo = f1 / f2;
cout <<\"The sum is \" << sum << endl << endl;
cout <<\"The ifference is \" << diff << endl << endl;
cout <<\"the product is \" << prod << endl << endl;
cout <<\"The quotient is\" << quo << endl << endl;
if (f1 == f2)
cout << \"fraction 1 equals fraction 2\";
else
cout << \"fraction 1 does not equal fraction 2\";
if (f1 != f2)
cout << \"fraction 1 does not equal faction 2\";
else
cout << \"fraction 1 equals fraction 2\";
cout << \"preincrement: \" << ++f1;
cout << \"postincrement: \" << f1++;
cout << \"after postincrement: \" << f1;
return 0;
}
Solution
Here is the partially developed code for you:
#include <iostream>
using namespace std;
class Fraction
{
private:
int numerator;
int denominator;
int whole;
public:
Fraction()
{
whole = numerator = 0;
denominator = 1;
}
Fraction(int w, int num, int denom)
{
whole = w;
numerator = num;
denominator = denom;
}
void toReciprocal()
{
numerator += whole * denominator;
whole = 0;
}
void print()
{
cout<<numerator / denominator<< \" \" <<numerator % denominator <<\" / \"<<denominator;
}
Fraction operator*(Fraction second)
{
Fraction temp;
this->toReciprocal();
second.toReciprocal();
temp.setNumerator(numerator + second.getNumerator());
temp.setDenominator(denominator + second.getDenominator());
return temp;
}
Fraction operator/(Fraction second)
{
Fraction temp;
this->toReciprocal();
second.toReciprocal();
temp.setNumerator(numerator * second.getDenominator());
temp.setDenominator(denominator * second.getNumerator());
return temp;
}
Fraction operator+(Fraction second)
{
Fraction temp;
this->toReciprocal();
second.toReciprocal();
temp.setNumerator(numerator*second.getDenominator() + denominator*second.getNumerator());
temp.setDenominator(denominator * second.getDenominator());
return temp;
}
Fraction operator-(Fraction second)
{
Fraction temp;
this->toReciprocal();
second.toReciprocal();
temp.setNumerator(numerator*second.getDenominator() - denominator*second.getNumerator());
temp.setDenominator(denominator * second.getDenominator());
return temp;
}
void setNumerator(int num)
{
numerator = num;
}
void setDenominator(int den)
{
denominator = den;
}
int getNumerator()
{
return numerator;
}
int getDenominator()
{
return denominator;
}
friend ostream &operator<<(ostream &, const Fraction &);
friend istream &operator>>(istream &, Fraction &);
};
ostream &operator<<( ostream &output, Fraction &c )
{
output<<c.numerator / c.denominator<< \" \" <<c.numerator % c.denominator <<\" / \"<<c.denominator;
return output;
}
istream &operator>>( istream &input, Fraction &c )
{
input >> c.whole >> c.numerator >> c.denominator;
return input;
}
int main() // make sure to simplify/reduce in operator >>
{
Fraction f1, f2, sum, diff, prod, quo;
cin >> f1;
cin >> f2;
cout << \"first fraction is \" << f1 << endl << endl;
cout << \"second fraction is \" << f2 << endl << endl;
sum = f1 + f2;
diff = f1 - f2;
prod = f1 * f2;
quo = f1 / f2;
cout <<\"The sum is \" << sum << endl << endl;
cout <<\"The ifference is \" << diff << endl << endl;
cout <<\"the product is \" << prod << endl << endl;
cout <<\"The quotient is\" << quo << endl << endl;
if (f1 == f2)
cout << \"fraction 1 equals fraction 2\";
else
cout << \"fraction 1 does not equal fraction 2\";
if (f1 != f2)
cout << \"fraction 1 does not equal faction 2\";
else
cout << \"fraction 1 equals fraction 2\";
cout << \"preincrement: \" << ++f1;
cout << \"postincrement: \" << f1++;
cout << \"after postincrement: \" << f1;
return 0;
}


