Overloading Operators Program

Any help you can give with this is greatly appreciated!

First, here are the instructions for the program:


Program 2: Overloading operators
Take the Fraction Class from program 1 (You may use my version or your own version) and overload the following operators: <<, >>, +, -, *, /, != and ==. Create a program that allows the user to choose the arithmetic operation that they want to perform. After selecting an operation the user should be presented with an arithmetic problem for which they should enter the correct answer. The answer DOES NOT have to be in reduced form, but you program should accept any correct answer. Ex 1/3, 3/9, and 6/18 are all correct answers. (Take care of this in the overloaded == operator)
The << operator should display fractions as numerator / denominator unless it is an improper fraction (numerator higher than the denominator) in which case it should be displayed as a mixed fraction. Ex. 11/3 should be displayed as 3 2/3. Improper fractions that will evaluate to a whole number should be displayed as just the whole number with no fraction part. Ex 10/2 should be displayed as 5.
The >> operator should read all fractions as numerator / denominator
Your program should use 4 fraction objects: Two for the operands, one for the calculated answer, and one for the users answer.


Here is the fraction class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
using namespace std;

class Fraction
{
	public:
		Fraction()
		{
			setNumerator(0);
			setDenominator(1);
		}
		
		Fraction(int n, int d)
		{
			setNumerator(n);
			setDenominator(d);
		}
		void setNumerator(int  n)
		{
			numerator = n;
		}
		void setDenominator(int d)
		{
			if (d == 0)
			{
				cout <<"ERROR: invalid denominator. Exiting  program...";
				exit(0);
			}
			else
			{
				denominator = d;
			}
		}
		int getNumerator(){return numerator;}
		int getDenominator(){return denominator;}
		void reduce()
		{
			int div = gcd(numerator, denominator);
			numerator = numerator / div;
			denominator = denominator / div;
		}
		void print(){cout << numerator <<"/"<< denominator;}
	private:
		int numerator, denominator;
		int gcd(int m, int n)
		{
            int r;
			if (m < n) 
				return gcd(n,m);
			
			r = m%n;
			if (r == 0) 
				return n;
			else 
				return(gcd(n,r));
		}					

};
#endif 


Here is the fraction driver:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "fraction.h"
#include <iostream>
#include <ctime>
int main()
{
	Fraction operand1, operand2, resultant;
	int n, d;
	char op, choice;
	srand(time(0));
	system("cls");
	cout << "***Welcome to Multiplying Fractions***"<<endl;
	
	do{
		n = 1 + (rand() % 11);
		d = 2 + (rand() % 11);
		operand1.setNumerator(n);
		operand1.setDenominator(d);
		
		n = 1 + (rand() % 11);
		d = 2 + (rand() % 11);
		operand2.setNumerator(n);
		operand2.setDenominator(d);
		
		n = operand1.getNumerator() * operand2.getNumerator();
		d = operand1.getDenominator() * operand2.getDenominator();
		
		resultant.setNumerator(n);
		resultant.setDenominator(d);
		resultant.reduce();
		
		do
		{
			cout <<"\n\nWhat is ";
			operand1.print();
			cout << " * ";
			operand2.print();
			cout << "?";
			cout<<"\n\n(Enter numerator / denominator)YOUR ANSWER MUST BE IN REDUCED FORM ->";
			cin >> n >> op >> d;
		}while(n != resultant.getNumerator() || d != resultant.getDenominator());
		
		
		cout <<"\nCORRECT!!! Enter q to quit, Enter c to continue ->";
		cin >> choice;
	}
	while(choice != 'q');
	
	return 0;
	
}


If someone could just point me in the right direction, I'd greatly appreciate it! Thank you.

¿how can I help you?
For one thing, I need help with the concept of how to overload the operators in the instructions. I know that they will go inside the loop, and I have to do the algorithm for the different fraction fucntions(addition/subtraction/multiply/divide)
http://www.cs.caltech.edu/courses/cs11/material/cpp/donnie/cpp-ops.html
Think of them as function with an special name.

For the insertion and extraction operators you can't use a member function
std::ostream& operator<<(std::ostream &out, const Fraction &b);
Last edited on
Topic archived. No new replies allowed.