Create a class for working with fractions Only 3 private dat
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 this program where the following operators are overloaded:
>> << + - * / == !=
also overload the preincrement and post increment operators ++ which will change a fraction to its reciprocal.
Solution
Here is the 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;
}
};
int main()
{
Fraction f3;
int choice, numerator, denominator, whole;
while(1)
{
cout<<\"Enter whole part of first fraction: \";
cin>>whole;
cout<<\"Enter the numerator of first fraction: \";
cin>>numerator;
cout<<\"Enter the denominator of first fraction: \";
cin>>denominator;
//f1.setNumerator(numerator);
//f1.setDenominator(denominator);
Fraction f1 (whole, numerator, denominator);
cout<<\"Enter whole part of second fraction: \";
cin>>whole;
cout<<\"Enter the numerator of second fraction: \";
cin>>numerator;
cout<<\"Enter the denominator of second fraction: \";
cin>>denominator;
//f2.setNumerator(numerator);
//f2.setDenominator(denominator);
Fraction f2 (whole, numerator, denominator);
cout<<\"Enter the operation to perform: 1.Addition. 2. Subtraction. 3. Multiplication. 4. Division. 5. Exit.\"<<endl;
cout<<\"Enter your choice: \";
cin>>choice;
switch(choice)
{
case 1: f3 = f1 + f2; break;
case 2: f3 = f1 - f2; break;
case 3: f3 = f1 * f2; break;
case 4: f3 = f1 / f2; break;
case 5: return 0;
}
f3.print();
}
}


