Project #8 Solution




C++ lets us define how the standard operators in the language work on objects of classes that we have created. For example, if you define a fraction class it would be nice to have the standard plus operator (+) work on objects of that class to add two fractions together. C++ allows us to do just that.

In this project, you will create a class to represent a fraction. A fraction has two integers, a numerator and a denominator. In your Fraction class you will overload a number of the standard operators.


To correctly overload the standard arithmetic operators, the equality operator, and the stream insertion operator for Fraction objects.

The Fraction Class

Create a class named Fraction. Your Fraction class will contain integers representing a fraction’s numerator and denominator. You should keep the numerator and denominator in lowest terms at the conclusion of every public member function, including the constructor. Also, denominators should never be negative, so you have to negate both parts if for some reason the denominator becomes negative. Create a private function to enforce these two conditions. The class should also contain the following member functions:

A parameterized constructor that takes, two integers for the numerator and denominator, respectively. Use default arguments as follows: the numerator defaults to 0 and the denominator defaults to 1. Do not allow a zero denoiminator (throw a runtime_error exception in that case). Note that the user can invoke the constructor with zero, one, or two arguments.

A function that overloads the plus operator (+).

A function that overloads the minus operator (-).

A function that overloads the multiplication operator (*).

A function that overloads the division operator (/).

A function that overloads the equality comparison operator (==).

Note that the operator functions must all be non-member functions, as discussed in the slides on operator overloading. Make them friends to your Fraction class.

If you do not remember how to do arithmetic on fractions, you may have to look this up. A good Algebra book should contain this information or you can use Google. When you are done, you should test your code to make sure that all of the operations work correctly.

In addition, you must write a stand-alone, friend function that overloads the stream insertion operator so that it outputs a fraction using a slash surrounded by spaces to separate the numerator and denominator (e.g., 3 / 4). However, if the denominator is 1, just output the numerator without a slash.

A driver has been provided for you to use. Please use this standard driver without change unless you implement the extra credit (in which case you may add statements to test that part). You can download the driver here.

Format and document your code in accordance with the course style guidelines. Include a file prologue identifying you as the author. Submit your project using the instructions outlined in the Course Syllabus, Programming Projects section.

File(s) to Submit:

Place the project folder for the project into a zip file. Submit the zip file to Canvas as project #7.

Grading Criteria


Points possible

Your points

Your program meets the grading guidelines:

Source code files contain a declaration that you did not copy any code

Code meets style guidelines

Code is properly documented


You have a parameterized constructor with two default arguments.


You keep the fraction in lowest terms after every public member function executes.


You ensure that the denominator is always a positive integer, without changing the mathematical value of the fraction.


You have a correctly functioning operator+.


You have a correctly functioning operator-.


You have a correctly functioning operator*.


You have a correctly functioning operator/.


You have a correctly functioning operator==.


You have a correctly functioning operator<<. Make sure that a denominator of 1 is never printed (and no slash in that case either).


(Extra credit: Implement the pre and post ++ and — operators.


– Late Penalty (20% per day)



Sample Output:

5 / 4

-1 / 4

3 / 8

2 / 3



Error: zero denominator

error: Content is protected !!