Part C - Encapsulation

Expressions

Workshop 6

In this workshop, you are to design a class that evaluates expressions involving fractions.

Learning Outcomes

Upon successful completion of this workshop, you will have demonstrated the abilities to

• overload an operator as a member function
• overload an operator as a helper function
• overload an operator as a friend function

Fraction Calculator - All Groups

Fraction Class

Define a class named Fraction that holds the data for a rational number.  The fraction may be positive or negative valued.  If you completed workshop 2, you may upgrade that solution to meet the specifications below.

Include in your class design the following functions:

• Fraction() - a no-argument constructor that sets the state of the current object to a safe empty state

• Fraction(int, int) - a two-argument constructor that receives the fraction's numerator and denominator

• void simplify() - a private member function that simplifies the numerator and denominator to lowest denominator form.

• void display() const - a query that displays the fraction in numerator/denominator form on standard output

• Fraction& operator+=(const Fraction& f) - a member operator that adds Fraction f to the current object - and returns a reference to the current object

• Fraction operator+(const Fraction&, const Fraction&) - a non-friend helper operator that adds two Fraction objects and returns a copy of the result

• bool operator==(const Fraction&, const Fraction&) - a friend operator that compares two Fraction objects for equality

Make sure to call your simplify() member function from your other functions whenever appropriate.

Place your class definition in a header file named Fraction.h and your function definitions in an implementation file named Fraction.cpp.

Client Module

The main module that uses your Fraction class is listed on the left, while the results of execution are listed on the right:

 ``` // Workshop 6 - Fraction Calculator // w6.cpp #include using namespace std; #include "Fraction.h" void read(const char* msg, Fraction& f) { int num, den; cout << "Enter " << msg; << endl; cout << " Numerator : "; cin >> num; cout << " Denominator : "; cin >> den; f = Fraction(num, den); } int main() { Fraction left, right, result, ref; cout << "Fraction Calculator\n"; cout << "===================\n"; read("Left Operand : ", left); read("Right Operand : ", right); cout << "Result : " << endl; left.display(); cout << " + "; right.display(); cout << " = "; result = left + right; result.display(); cout << endl; read("2nd Right Operand : ", right); cout << "Result : " << endl;  result.display(); cout << " += "; right.display(); cout << " => "; result += right; result.display(); cout << endl; read("Reference : ", ref); if (result == ref) cout << "Result == Reference" << endl; else cout << "Result != Reference" << endl;  } ``` ``` Fraction Calculator =================== Enter Left Operand : Enter numerator : 1 Enter denominator : 6 Enter Right Operand : Enter numerator : 1 Enter denominator : 3 Result : 1/6 + 1/3 = 1/2 Enter 2nd Right Operand :  Enter numerator : 1 Enter denominator : 3 Result : 1/2 += 1/3 => 5/6 Enter Reference : Enter numerator : 10 Enter denominator : 12 Result == Reference ```

Design and code the Fraction class described above.  Add to your class design the following member functions:

• Fraction& operator-=(const Fraction& f) - a member operator that subtracts Fraction f from the current object and returns a reference to the current object

• Fraction operator*=(const Fraction& f) - a member operator that multiplies the current object by Fraction f and returns a reference to the current object

• Fraction operator/=(const Fraction& f) - a member operator that divides the current object by Fraction f and returns a reference to the current object

Main Program

The main module that uses your Fraction class is listed on the left, while the results of execution are listed on the right:

 ``` // Workshop 6 - Fraction Calculator // w6.cpp #include #include using namespace std; #include "Fraction.h" void read(const char* msg, Fraction& f) { int num, den; cout << "Enter " << msg << endl; cout << " Numerator : "; cin >> num; cout << " Denominator : "; cin >> den; f = Fraction(num, den); cin.ignore(); } int main() { char op; bool quit = false; Fraction left, right, result, ref; cout << "Fraction Calculator\n"; cout << "===================\n"; read("Left Operand : ", left); do { cout << "+= -= *= /= ## to quit : "; cin.get(op, 3); char c = cin.get(); if (c != '\n' || op != '=' && op != '#') { cerr << "Try Again!" << endl; cin.ignore(2000, '\n'); } else if (strcmp(op, "##") == 0) { read("Reference : ", ref); quit = true; } else { read("Right Operand : ", right); cout << "Result : " << endl;  left.display(); switch (op) { case '+': cout << " += "; left += right; break; case '-': cout << " -= "; left -= right; break; case '*': cout << " *= "; left *= right; break; case '/': cout << " /= "; left /= right; break; } right.display(); cout << " => "; left.display(); cout << endl; } } while (!quit); if (left == ref) cout << "Result == Reference" << endl; else cout << "Result != Reference" << endl; }``` ``` Fraction Calculator =================== Enter Left Operand : Numerator : 1 Denominator : 6 += -= *= /= ## to quit : +=  Enter Right Operand : Numerator : 1 Denominator : 3 Result : 1/6 += 1/3 => 1/2 += -= *= /= ## to quit : *= Enter Right Operand : Numerator : 1 Denominator : 3 Result : 1/2 *= 1/3 => 1/6 += -= *= /= ## to quit : -= Enter Right Operand : Numerator : 1 Denominator : 12 Result : 1/6 -= 1/12 => 1/12 += -= *= /= ## to quit : /= Enter Right Operand : Numerator : 3 Denominator : 4 Result : 1/12 /= 3/4 => 1/9 += -= *= /= ## to quit : ## Enter Reference : Numerator : 2 Denominator : 18 Result == Reference ```

SUBMISSION

Typescript

Create a typescript of your complete solution using the following commands:

 ``` + At the prompt, type: script w6.txt + At the prompt, type: whoami + At the prompt, type: cat Fraction.h Fraction.cpp + At the prompt, type: g++ -o w6 w6.cpp Fraction.cpp  + At the prompt, type: w6 + At the input prompt, enter test data + At the prompt type: exit ```

Moodle

• Select W6 under Assignments and Tasks
• Write a short note to your instructor
• press "Edit"
• press "Save Changes"
• When ready to submit, press "Send for Marking"

Other Submissions  