Operator overloading question

Hye so this is a code which I found on the internet.We have to add, subtract multiply rational numbers but with operator overloading. I am not getting what he did at line 169 and why he is using scope resolution operator at line 25. Kindly explain it to me. Thanks

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
  #include<iostream>
#include<cstdlib>
using namespace std;
class Rational1
{
	int numerator, denominator;
public:
	Rational1();
	Rational1(int, int);
	Rational1 operator+(Rational1);
	Rational1 operator-(Rational1);
	Rational1 operator*(Rational1);
	Rational1 operator/(Rational1);
	bool operator>(Rational1);
	bool operator<(Rational1);
	bool operator>=(Rational1);
	bool operator<=(Rational1);
	bool operator==(Rational1);
	bool operator!=(Rational1);
	void printRational1();
	void reduce();
};

// default constructor: parameters are numerator and denominator respectively
Rational1::Rational1(int n, int d) {
	numerator = n;
	denominator = d;
	if (d == 0 || d<0)
	{
		cout << "Cannot enter zero or negative numbers for denominator" << endl;
		exit(1);
	}
	reduce();
}
Rational1::Rational1() {
	numerator = 0;
	denominator = 1;
}

//--------------------------------- add --------------------------------------
// overloaded +: addition of 2 Rational1s, current object and parameter
Rational1 Rational1::operator+(Rational1 a) {
	Rational1 t;
	t.numerator = a.numerator * denominator + a.denominator * numerator;
	t.denominator = a.denominator * denominator;
	t.reduce();
	return t;
}

//------------------------------ subtract ------------------------------------
// subtraction of 2 Rational1s, current object and parameter

Rational1 Rational1::operator-(Rational1 s) {
	Rational1 t;
	t.numerator = s.denominator * numerator - denominator * s.numerator;
	t.denominator = s.denominator * denominator;
	t.reduce();
	return t;
}

//------------------------------ multiply ------------------------------------
// multiplication of 2 Rational1s, current object and parameter
Rational1 Rational1::operator*(Rational1 m) {
	Rational1 t;

	t.numerator = m.numerator * numerator;
	t.denominator = m.denominator * denominator;
	t.reduce();
	return t;
}

//-------------------------------- divide ------------------------------------
// division of 2 Rational1s, current object and parameter,
// division by zero crashes
Rational1 Rational1::operator/(Rational1 v) {
	Rational1 t;

	t.numerator = v.denominator * numerator;
	t.denominator = denominator * v.numerator;
	t.reduce();

	return t;
}
bool Rational1::operator>(Rational1 v) {
	float f1 = numerator / float(denominator);
	float f2 = v.numerator / float(v.denominator);
	if (f1>f2)
		return true;
	else return false;
}
bool Rational1::operator<(Rational1 v) {
	float f1 = numerator / float(denominator);
	float f2 = v.numerator / float(v.denominator);
	if (f1<f2)
		return true;
	else return false;

}
bool Rational1::operator>=(Rational1 v) {
	float f1 = numerator / float(denominator);
	float f2 = v.numerator / float(v.denominator);
	if (f1 >= f2)
		return true;
	else return false;

}
bool Rational1::operator<=(Rational1 v) {
	float f1 = numerator / float(denominator);
	float f2 = v.numerator / float(v.denominator);
	if (f1 <= f2)
		return true;
	else
		return false;

}
bool Rational1::operator==(Rational1 v) {
	float f1 = numerator / float(denominator);
	float f2 = v.numerator / float(v.denominator);
	if (f1 == f2)
		return true;
	else
		return false;
}
bool Rational1::operator!=(Rational1 v) {
	float f1 = numerator / float(denominator);
	float f2 = v.numerator / float(v.denominator);
	if (f1 != f2)
		return true;
	else return false;
}
//---------------------------- printRational1 ---------------------------------
void Rational1::printRational1() {
	if (denominator == 0)
		cout << endl << "DIVIDE BY ZERO ERROR!!!" << endl;
	else if (numerator == 0)
		cout << 0;
	else
		cout << numerator << "/" << denominator;
}

//-------------------------------- reduce ------------------------------------
// reduce fraction to lowest terms
void Rational1::reduce() {
	int n = numerator < 0 ? -numerator : numerator;
	int d = denominator;
	int largest = n > d ? n : d;
	int gcd = 0; // greatest common divisor

	for (int loop = largest; loop >= 2; loop--)
		if (numerator % loop == 0 && denominator % loop == 0) {
			gcd = loop;
			break;
		}

	if (gcd != 0) {
		numerator /= gcd;
		denominator /= gcd;
	}
}
int main()
{
	Rational1 r1, r2, r3;
	int n, d;
	int choice;
	char ch;
	cout << "Enter in format :numerator <space> denominator\n";
	cout << "Enter first Rational1 number - numerator and denominator\t: ";
	cin >> n >> d;
	r1 = Rational1(n, d);
	cout << "Enter second Rational1 number - numerator and denominator\t:";
	cin >> n >> d;
	r2 = Rational1(n, d);
	do
	{
		cout << "**********MENU***********\n";
		cout << "1. +\t2. -\t3.*\t4. /\t5. >\t6. <\n7. >=\t8. <=\t9. !=\t10. ==";
		cout << "\n11. Print\t12.Exit\n";
		cout << "Enter your choice";
		cin >> choice;
		switch (choice)
		{
		case 1:
			r3 = r1 + r2;
			r3.printRational1();
			break;
		case 2:
			r3 = r1 - r2;
			r3.printRational1();
			break;
		case 3:
			r3 = r1*r2;
			r3.printRational1();
			break;
		case 4:
			r3 = r1 / r2;
			r3.printRational1();
			break;
		case 5:
			if (r1>r2)
			{
				r1.printRational1();
				cout << " is greater than ";
				r2.printRational1();
			}
			else
			{
				r1.printRational1();
				cout << " is not greater than ";
				r2.printRational1();
			}
			break;
		case 6:
			if (r1<r2)
			{
				r2.printRational1();
				cout << " is greater than ";
				r1.printRational1();
			}
			else
			{
				r2.printRational1();
				cout << " is not greater than ";
				r1.printRational1();
			}
			break;
		case 7:
			if (r1 >= r2)
			{
				r1.printRational1();
				cout << " is greater than or equal to ";
				r2.printRational1();
			}
			else
			{
				r1.printRational1();
				cout << " is not greater than or equal to ";
				r2.printRational1();
			}
			break;
		case 8:
			if (r1 <= r2)
			{
				r2.printRational1();
				cout << " is greater than or equal to ";
				r1.printRational1();
			}
			else
			{
				r2.printRational1();
				cout << " is not greater than or equal to ";
				r1.printRational1();
			}
			break;
		case 9:
			if (r1 != r2)
			{
				r1.printRational1();
				cout << " is not equal to ";
				r2.printRational1();
			}
			else
			{
				r1.printRational1();
				cout << " is  equal to ";
				r2.printRational1();
			}
			break;
		case 10:
			if (r1 == r2)
			{
				r1.printRational1();
				cout << " is equal to ";
				r2.printRational1();
			}
			else
			{
				r1.printRational1();
				cout << " is not equal to ";
				r2.printRational1();
			}
			break;
		case 11:
			cout << "\nFirst Rational Number\n";
			r1.printRational1();
			cout << "\nSecond Rational Number\n";
			r2.printRational1();
			cout << "\nThird Rational Number\n";
			r3.printRational1();
			break;
		case 12:
			exit(0);;
		}
		cout << "\nDo you want to continue(y/n)\n";
		cin >> ch;
	} while (ch == 'y' || ch == 'Y');
	return 0;
	system("pause");
}
Last edited on
I am not getting what he did at line and why he is using scope resolution operator at line .


You missed out the line numbers.
I have written it now!
why he is using scope resolution operator at line 25.


It's outside the class declaration, so How else would you know it's a class function? If the scope resolution operator wasn't there, it wouldn't be a class function. It would just be a function.
I am not getting what he did at line 169


This line: r1 = Rational1(n, d);

He's creating an object of type Rational1 using the constructor Rational1::Rational1(int n, int d) and then setting r1 to be equal to whatever what he just created. He could have written this instead: Rational1 r1(n, d);
Why he is defining it outside the class. Why not simply inside the class?
That's just a choice he made.

Maybe he's thinking about moving the implementation into a different file, so that other code files can use this class with a #include that only includes the declaration.

Maybe this is just toy code that doesn't matter.
Style choice with practical benefit.

The public members are introduced compactly within 14 lines. These are the interface of the Rational1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public:
	Rational1();
	Rational1(int, int);
	Rational1 operator+(Rational1);
	Rational1 operator-(Rational1);
	Rational1 operator*(Rational1);
	Rational1 operator/(Rational1);
	bool operator>(Rational1);
	bool operator<(Rational1);
	bool operator>=(Rational1);
	bool operator<=(Rational1);
	bool operator==(Rational1);
	bool operator!=(Rational1);
	void printRational1();
	void reduce();

If you are about to use a class, you want to know how to use it. What members are there for you to use? Here you can see them all with one glance. Some are (hopefully) self-explanatory. For some you need to peek documentation to know some details.

The implementation details are of no interest to the user, as long as they do what the class interface promises.
Topic archived. No new replies allowed.