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

Objectives: This assignment will help you practice writing a program to overload

ID: 3685888 • Letter: O

Question

Objectives: This assignment will help you practice writing a program to overload different operators, or in other words to make the operator perform differently that learned from the lecture notes so far.

Problem Description:

Rational fractions are of the form a / b, in which a and b are integers and ! . In this exercise, by ‘‘fractions’’ we mean rational fractions. Suppose a / b and c / d are fractions. Arithmetic operations and Relational Operators on fractions are defined by the following rules:

Arithmetic Operators:

a/b + c/d = (ad + bc) / bd

a/b + c/d = (ad - bc) / bd

a/b x c/d = ac / bd

(a/b)/(c/d) = ad/ bc , in which c/d can't = 0.

! Relational Operators:

(a/b<=c/d) = (ad<=bc)

(a/b>=c/d) = (ad>=bc)

(a/b==c/d) = (ad==bc)

(a/b!=c/d) = (ad!=bc)

Fractions are compared as follows: a / b op c / d if ad op bc, in which op is any of the relational operations. For example, a / b < c / d if ad < bc. In this assignment, you will need to design a class— say, fractionType which performs the arithmetic and relational operations on fractions. Overload the arithmetic and relational operators so that the appropriate symbols can be used to perform the operation. Also, overload the stream insertion and stream extraction operators for easy input and output. Write a C++ program that, using the class fractionType, performs operations on fractions. Among other things, test the following: Suppose x, y, and z are objects of type fractionType. If the input is 2/ 3, the statement: cin >> x; should store 2/ 3 in x. The statement: cout << x + y << endl; should output the value of x + y in fraction form. The statement: z = x + y; should store the sum of x and y in z in fraction form. You are given the main driver for this program “MainProgram.cpp” which you can find in the folder “Supporting files for Assg_9”. A sample run of the program is provided below.

Explanation / Answer

               bool operator==(rational);

bool operator!=(rational);

  bool operator>(rational);

  bool operator<(rational);

  bool operator>=(rational);

  bool operator<=(rational);

bool rational::operator==(rational c1)

{

  return (c1d.num == numer) && (c1.den == denom);

}

bool rational::operator!=(rational c1)

{

return (c1.numer != numer) && (c1.denom != denom);

}

bool rational::operator>(rational c1)

{

return (numer *c1.denom) > (c1.numer * denom);

}

bool rational::operator<(rational c1)

{

return (numer * c1.denom) < (c1d.numer * denom);

}

bool rational::operator>=(rational c1)

{

  return (numer* c1.denom) >= (c1.numer * denom);

}

bool rational::operator<=(rational c1)

{

return (numer *c1.denom) <= (c1.numer * denom);

}

            getch();