CSCI 136 |
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 Item | Points Possible | Points Earned |
---|---|---|
Program Compiles | 1 | |
Program Runs | 1 | |
Comments on all classes and methods | 2 | |
Two constructor methods | 2 | |
Assignment operation | 3 | |
Test for equality | 3 | |
Print to screen | 2 | |
Reduce to lowest terms | 3 | Reciprocal | 1 |
Math operators (all 4) | 12 | |
Total | 30 |
Page last updated: January 06, 2022