Overloading Rational Numbers code

Apr 18, 2019 at 8:51am
I have this code that I found online that I need adopting so that the functions are overloaded operators, instead of just regular functions.

But Im struggling a bit, as Im not sure where to begin and was hoping someone could point me in the right direction.

The commented out sections is what I tried, thinking that would work...

Main.cpp:
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
62
63
64
65
66
#include <iostream>
#include "rationalNumber.h"

using namespace std;

int main() {

	rationalNumber c(10, 15), z, x(10,15); //creates 3 objects with values

		c.printRational();
		cout << " + ";
		x.printRational();
		cout << " = ";
		z = c.addition(x);
		z.printRational();
		cout << endl;

		c.printRational();
		cout << " - ";
		x.printRational();
		cout << " = ";
		z = c.subtraction(x);
		z.printRational();

		cout << endl;
		c.printRational();
		cout << " * ";
		x.printRational();
		cout << " = ";
		z = c.multiplication(x);
		z.printRational();
                cout << endl;

		c.printRational();
		cout << " / ";
		x.printRational();
		cout << " = ";
		z = c.division(x);
		z.printRational();


		fflush(stdin);
		cin.get();
return 0;
}


/*

Need to overload the following:

"+"
"-"
"*"
"/"
"="
"=="
"!="
">"
"<"
">="
"<="
">>"
"<<"
*/


rationalNumber.h:
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
#ifndef RATIONALNUMBER_H
#define RATIONALNUMBER_H

class rationalNumber
{
	public:
		rationalNumber(int num = 0, int den = 1);
		rationalNumber addition(rationalNumber c);
		rationalNumber subtraction (rationalNumber c);
		rationalNumber multiplication (rationalNumber c);
		rationalNumber division (rationalNumber c);

		void printRational();
		void printRationalAsDouble();

		//rationalNumber operator+(rationalNumber c);

	private:
		int numerator;
		int denominator;
		void reduction();

};

#endif 



rationalNumber.cpp:
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "rationalNumber.h"
#include <iostream>

using namespace std;

rationalNumber::rationalNumber(int num, int den) : numerator(num), denominator(den){
}

/*
rationalNumber operator+(rationalNumber c){
	rationalNumber temp;
	temp.numerator = (numerator * c.denominator) + (c.numerator * denominator);// adds numerator
	temp.denominator = denominator * c.denominator;// add denominator
	temp.reduction();
	return temp;
}
*/


rationalNumber rationalNumber::addition(rationalNumber c){
	rationalNumber temp;
	temp.numerator = (numerator * c.denominator) + (c.numerator * denominator);// adds numerator
	temp.denominator = denominator * c.denominator;// add denominator
	temp.reduction();
	return temp;
}

rationalNumber rationalNumber::subtraction(rationalNumber c){
	rationalNumber temp;

	temp.numerator = (numerator * c.denominator) - (c.numerator * denominator);// adds numerator
	temp.denominator = denominator * c.denominator;// add denominator
	temp.reduction();
	return temp;
}

rationalNumber rationalNumber::multiplication(rationalNumber c){
	rationalNumber temp;
	temp.numerator = numerator * c.numerator; // multiplies numerator
	temp.denominator = denominator * c.denominator; // multiplies denominator
	temp.reduction();
	return temp;
}

rationalNumber rationalNumber::division(rationalNumber c){
	rationalNumber temp;
	temp.numerator = numerator * c.denominator; // divides numerator and denominator
	temp.denominator = denominator * c.numerator; // divdes denominator and numerator
	temp.reduction();
	return temp;
}

void rationalNumber::printRational(){
	cout <<numerator << "/" <<denominator;
}

void rationalNumber::printRationalAsDouble(){
	cout << (double) numerator / denominator;
}


void rationalNumber::reduction()
{
   int largest; // initializes largest
   largest = numerator > denominator ? numerator : denominator;

   int gcd = 1; // greatest common divisor

   for ( int loop = 2; loop <= largest; loop++ ) // initializes loop to 2, if loop is less that or equal to the largest, then increment the loop counter.

       if ( numerator % loop == 0 && denominator % loop == 0 ) //if the loop's remainder equals 0 and the denominator's remainder equals 0,
          gcd = loop; // then the greatest common denominator is assigned the value of the loop.

   if (gcd != 0)     {					// if the greatest common denominator does not equal 0, the divide both the numerator and the denominator by the greatest common denominator.
      numerator /= gcd;
      denominator /= gcd;

   } // end if


} // end function reduction


The assignment that I have is, that I have to create a program that can calculate rational numbers using the overloaded operators. I.e. +, - etc. This program works perfectly, but I need it to use operators, rather than functions if that makes sense.

Any help is appreciated.
Apr 18, 2019 at 9:08am
Since there is nothing but the class involved in this operation you can directly into operators:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class rationalNumber
{
	public:
		rationalNumber(int num = 0, int den = 1);
		rationalNumber addition(rationalNumber c);
		rationalNumber subtraction (rationalNumber c);
		rationalNumber multiplication (rationalNumber c);
		rationalNumber division (rationalNumber c);

		void printRational();
		void printRationalAsDouble();

		rationalNumber operator+(rationalNumber c);

	private:
		int numerator;
		int denominator;
		void reduction();

};
1
2
3
4
5
6
7
rationalNumber rationalNumber::operator+(rationalNumber c){
	rationalNumber temp;
	temp.numerator = (numerator * c.denominator) + (c.numerator * denominator);// adds numerator
	temp.denominator = denominator * c.denominator;// add denominator
	temp.reduction();
	return temp;
}
Or just calling addition(...)
1
2
3
rationalNumber rationalNumber::operator+(rationalNumber c){
	return addition(c);
}
Apr 18, 2019 at 10:29am
Why is it that the simplest things always go over my head???

Thanks @coder777 this was so simple yet I couldnt figure it out...
Topic archived. No new replies allowed.