Lab 8 - Operator Overloading
Due: APril 17 11:55pm
Moodle Link |
 |
Your goal is to create a class for storing and manipulating fractions. We wish to write code to add, subtract, multiply, and divide fractions. In addition we wish to overload the traditional binary arithemitic operators so that we can create code like below:
int main() {
int n, d;
cout << "Fraction test program." << endl;
char again;
do {
cout << "Enter numerator and denominator of first fraction:";
cin >> n >> d;
Fraction a(n, d);
cout << "Enter numerator and denominator of second fraction:";
cin >> n >> d;
Fraction b(n, d);
Fraction sum = a + b;
Fraction diff = a - b;
Fraction prod = a*b;
Fraction quotient = a / b;
// Display Results
cout << a.to_string() << " + " << b.to_string() << " = " << sum.to_string() << endl;
cout << a.to_string() << " - " << b.to_string() << " = " << diff.to_string() << endl;
cout << a.to_string() << " * " << b.to_string() << " = " << prod.to_string() << endl;
cout << a.to_string() << " / " << b.to_string() << " = " << quotient.to_string() << endl;
cout << endl << "Go again? (Y or y to continue):";
cin >> again;
} while (again == 'Y' || again == 'y');
return 0;
}
Fraction test program.
Enter numerator and denominator of first fraction:1 2
Enter numerator and denominator of second fraction:3 4
1/2 + 3/4 = 5/4
1/2 - 3/4 = 1/-4
1/2 * 3/4 = 3/8
1/2 / 3/4 = 2/3
Go again? (Y or y to continue):y
Enter numerator and denominator of first fraction:1 10
Enter numerator and denominator of second fraction:1 20
1/10 + 1/20 = 3/20
1/10 - 1/20 = 1/20
1/10 * 1/20 = 1/200
1/10 / 1/20 = 2/1
Go again? (Y or y to continue):y
Enter numerator and denominator of first fraction:23 57
Enter numerator and denominator of second fraction:101 31
23/57 + 101/31 = 6470/1767
23/57 - 101/31 = 5044/-1767
23/57 * 101/31 = 2323/1767
23/57 / 101/31 = 713/5757
Go again? (Y or y to continue):n
Consider the following code for representing fractions.
Fraction.h
#include <string>
using namespace std;
class Fraction
{
public:
// Constructors and Destructor
Fraction(void);
Fraction(int n, int d);
// Test functions
string to_string();
// Overloaded operators
// +, -, *, and /
private:
int numerator; //the "top" of the fraction
int denominator; //the "bottom" of the fraction
// PRE:
// POST: Fraction is simplified, e.g. 3/9, simplifies to 1/3
void simplify();
// PRE:
// POST: Returns the greatest common divisor of a and b
int gcd(int a, int b)const;
// Friend functions
friend Fraction add(Fraction a, Fraction b);
};
Fraction.cpp
#include <iostream>
#include "Fraction.h"
using namespace std;
// Default constructor
Fraction::Fraction(void)
{
numerator = 0;
denominator = 1;
}
// Constructor
Fraction::Fraction(int n, int d)
{
if (d == 0){
denominator = 1;
numerator = 0;
}else if (d < 0){
denominator = d * -1;
if (n > 0){
numerator = n * -1;
}
}else{
numerator = n;
denominator = d;
}
}
// Returns the greatest common divisor of two integers
int Fraction::gcd(int a, int b)const{
while (b != 0){
int temp = b;
b = a % b;
a = temp;
}
return a;
}
// Simplifies the calling fraction
void Fraction::simplify(){
int factor = gcd(numerator, denominator);
numerator = numerator / factor;
denominator = denominator / factor;
}
//test functions
Fraction add(Fraction a, Fraction b) {
int num = a.numerator * b.denominator + b.numerator * a.denominator;
int den = a.denominator * b.denominator;
Fraction result(num,den);
result.simplify();
return result;
}
string Fraction::to_string() {
string result = std::to_string(numerator) + "/" + std::to_string(denominator);
return result;
}
// Overloaded Operators
/*
* In each case: (num1/den1) op (num2/den2)
*
* PLUS (+) OPERATOR
* result = (num1 * den2 + num2 * den1) / (den1 * den2)
*
* MINUS (-) OPERATOR
* result = (num1 * den2 - num2 * den1) / (den1 * den2)
*
* MULIPLICATION (*) OPERATOR
* result = (num1 * num2) / (den1 * den2)
*
* DIVISION (/) OPERATOR
* result = (num1 * den2) / (den1 * num2)
*
*/
main.cpp
#include <iostream>
#include "Fraction.h"
using namespace std;
int main() {
int n,d;
cout << "Fraction test program." << endl;
cout << "Enter numerator and denominator of first fraction:";
cin >> n >> d;
Fraction a(n,d);
cout << "Enter numerator and denominator of second fraction:";
cin >> n >> d;
Fraction b(n,d);
Fraction c = add(a,b);
//Will be changed to use + operator
//Fraction c = a+b;
// Display Result
cout << a.to_string() << " + " << b.to_string() << " = " << c.to_string() << endl;
return 0;
}
Runs to turn in:
Fraction 1 | Fraction 2 |
1/2 | 3/5 |
52/43 | 63/21 |
3542/431 | 45785/3241 |
8/3 | 123/43 |
Grading Table
Requirement | Grading Comments | Points | Score |
Easy to use user interface | | 10 | |
C++ code includes comments, with project information at top, pre and post conditions for each functions and other cmments as needed. | | 10 | |
The C++ code has good formatting, indentation, and organization. | | 10 | |
Good variable and function names, appropriate use of constants rather then literal numbers. | | 10 | |
Classes: Correctly defined and well organized | | 20 | |
Runs: Run examples from trials with correct output | | 40 | |
Total | | 100 | |
Solutions