Given a class Fraction with the code snippets below. In C++. class Fraction { pu
ID: 3676304 • Letter: G
Question
Given a class Fraction with the code snippets below. In C++.
class Fraction
{
public :
Fraction () ;
Fraction ( int n, int d) ;
void setNumerator ( int n);
void setDenominator ( int d) ;
int getNumerator () ;
int getDenominator () ;
/* Add your code below */
private :
int num ; // numerator
int den ; // denominator
}
int main ()
{
Fraction x, y;
/* Add your code below */
return 0;
}
/* Function Definitions go here *
/* Program ends */
1) Rewrite the class and overload the following operators and give both their prototypes (declarations) and definitions.
2) Give the definitions after the main function outside of the class definition (0 point if only the prototype is given).
a) + /*recall that a/b + c/d = ad+bc/bd
b)
c)
d) / *recall that a/b ÷ c/d = a/b d/c */
e) == /*recall that a/b = c/d if ad = bc */
f) >> /* the user will type 5/7 to mean 5/7 */
g) << /* prints out the fraction using the forward slash as the fraction bar */
Explanation / Answer
{
public :
Fraction () ;
Fraction ( int n, int d)
{
num = n;
if (d==0)
{
cout << "ERROR: ATTEMPTING TO DIVIDE BY ZERO" << endl;
exit(0); // will terminate the program if division by 0 is attempted
}
else
denominator = d;
}
void setNumerator ( int n);
void setDenominator ( int d) ;
int getNumerator () ;
int getDenominator () ;
private :
int num ; // numerator
int den ; // denominator
Fraction Sum(Fraction otherFraction)
{
int n = num*otherFraction.den+otherFraction.num*den;
int d = den*otherFraction.den;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
Fraction Difference(Fraction otherFraction)
{
int n = num*otherFraction.den-otherFraction.num*den;
int d = den*otherFraction.den;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
Fraction Product(Fraction otherFraction)
{
int n = num*otherFraction.num;
int d = den*otherFraction.den;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
Fraction Division(Fraction otherFraction)
{
int n = num*otherFraction.den;
int d = den*otherFraction.num;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
Fraction Equality(Fraction otherFraction)
{
int n = num*otherFraction.num;
return Fraction(num);
}
Fraction RightShift()
{
return Fraction(num);
}
Fraction LeftShift()
{
return Fraction(num);
}
int gcd(int n, int d)
{
int remainder;
while (d != 0)
{
remainder = n % d;
n = d;
d = remainder;
}
return n;
}
void show() // Display method
{
if (den == 1) // e.g. fraction 2/1 will display simply as 2
cout << num << endl;
else
cout << num << "/" << den << endl;
}
};
int Fraction::getNumerator()
{
return num;
}
int Fraction::getDenominator()
{
return den;
}
void Fraction::setNumerator(const int numer)
{
num = numer;
return;
}
bool Fraction::setDenominator(const int denom)
{
bool set = false;
if (denom != 0) // prohibits setting denominator to zero!
{
set = true;
den = denom;
}
return set;
}
int main ()
{
Fraction x(1,2)
Fraction z(1,4);
Fraction y;
y = z.Sum(x);
y.show();
y = z.Difference(x);
y.show();
y = z.Product(x);
y.show();
y = z.Division(x);
y.show();
y= z.Equality(x);
cout<<y;
y=z.RightShift();
y.show();
y=z.LeftShift();
y.show();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.