Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

General math) a. In Chapter 11\'s programming projects, you were asked to constr

ID: 3590171 • Letter: G

Question

General math) a. In Chapter 11's programming projects, you were asked to construct a Fraction class that could be used to add, subtract, multiply, and divide two fractions. For this exercise, write an overloaded friend ostream insertion operator function that allows a Fraction object to be output and appear in the form num/denom, using a cout statement. b. Write an overloaded friend istream extraction operator function that allows fractions in the formm a/b . to be input and assigned to a Fraction object

Explanation / Answer

friend istream& operator >> (istream& in, Fraction& f)
// input f in the form of a/b, where b cannot be zero. Also,
// if b is negative, the Fraction will change b to be positive.
// So, again, 1/-3 will be changed to -1/3
{
   char temp;
   in >> f.num >> temp >> f.den;
   if(f.den < 0)
   {
      f.num *= -1;
       f.den *= -1;
   }  
   return in; // enables cascading eg cout << a << b;
   }
  
friend ostream& operator << (ostream& out, Fraction& f)
// output a Fraction f in form of a/b
{
   out << f.num << " / " << f.den;
   return out; // enables cascading eg cout << a << b;
   }