CSCI 136
Fundamentals of Computer Science II
Spring 2022

Montana Tech of The University of Montana
Computer Science & Software Engineering

ASSIGNMENT #12 - Fraction Math - C++ Classes and Operator Overloading

In this assignment, you will be you will be practicing using C++ classes by building a fraction class and defining the functions (using overloaded operators) that you might want to use on fractions.

Fractions. Last semester, in CSCI 135, we looked at how Python classes could be used to define a Fraction class. The slides for that class can be found here, if you need a refresher. For this assignment, you are to recreate that Fraction class in C++, and take advantage of the capability to overload operators that you couldn't do in Python. Name this program Fraction.cpp.

Your program should implement the following API (yes, API means Application Programming Interface, and it's not just a Python thing):

class Fraction
         Fraction(int x, int y)  // create a fraction with numerator x and denominator y  
         Fraction()              // create a fraction with numerator 1 and denominator 1
Fraction operator=(Fraction f)   // assign the value of one fraction to another
bool     operator==(Fraction f)  // check to see if two fractions are equal
void     print()                 // print the fraction to the screen in the form "n/d"
Fraction reduce()                // reduce the fraction to its lowest terms
Fraction reciprocal()            // return the reciprocal of a fraction
Fraction operator+(Fraction f)   // add two fractions
Fraction operator-(Fraction f)   // subtract the second fraction from the first
Fraction operator*(Fraction f)   // multiply two fractions
Fraction operator/(Fraction f)   // divide the first fraction by the second

You should also implement a main method in your program that creates fractions and tests the various functions. We, of course, will be doing the same to test that your Fraction class works correctly. Be sure to look through the grading criteria below to make sure you've completed everthing.

Good luck -- and have fun!

Grade ItemPoints PossiblePoints Earned
Program Compiles
Program Runs
Comments on all classes and methods
Two constructor methods
Assignment operation
Test for equality
Print to screen
Reduce to lowest terms
Math operators (all 4)

Submission. Submit your code, Fraction.cpp, using Moodle. Be sure your submitted code has the required header with your name and a description of the program. Remember! All your classes and methods, along with a header, must be commented!

Page last updated: January 06, 2022