Objective:

To learn about operator overloading.

Description:

Consider the following class definition:

class Fraction
{
private:
int numerator;
int denominator;

public:
Fraction(int n = 0, int d = 1) {numerator = n; denominator = d;}
};

and the following main function:

int main()
{
Fraction X, Y, Z;

cout << "Enter two fractions (in the form numerator/denominator): ";
cin >> X;
cin >> Y;

Z = X + Y;
cout << X << " + " << Y << " = " << Z << endl;
Z = X - Y;
cout << X << " - " << Y << " = " << Z << endl;
Z = X * Y;
cout << X << " * " << Y << " = " << Z << endl;
Z = X / Y;
cout << X << " / " << Y << " = " << Z << endl;

cout << X << " as a double is " << (double)X << endl;

system("pause");
return 0;
}

Write the code that would be necessary to get this program to compile and work correctly (i.e. you should overload the appropriate operators for the Fraction class).

Requirements:

You must write separate .h files for each class.

You should also use good commenting and code style. Expect points to be deducted from your score if you do not comment your code and use good coding practices.

You may not use global variables or GOTO statements.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.