Im having a problem with classes Please help

So I am writing a program that is supposed to produce a rational number (numerator/denominator) and the program works until I need to call .reduce() I am pretty sure that the problem is syntax or how I call int gCD inside of reduce() someone please help

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
#include <iostream>
#include <iomanip>
using namespace std;

class RationalNumber{
private:
	int numer;
	int denom;
	int gCD(int num1, int num2);
	void reduce();
public:
	RationalNumber(int num1, int num2);
	RationalNumber(int num1);
	RationalNumber add(RationalNumber& num);
	RationalNumber sub(RationalNumber num);
	RationalNumber mul(RationalNumber num);
	RationalNumber div(RationalNumber num);
	bool equals(RationalNumber num);
	bool  less_than(RationalNumber num);
	bool greater_than(RationalNumber num);
	RationalNumber input(istream& in);
	void output(ostream& out);
};
void RationalNumber::reduce()
{
	int r = 0, num1 = abs(numer), num2 = abs(denom);

	r = gCD(num1, num2);

	numer = numer / r;

	denom = denom / r;
}
int gCD(int num1, int num2)
{
	while (num1 != num2)
	{
		if (num1 > num2)
			num1 = num1 - num2;
		else
			num2 = num2 - num1;
	}

	return num1;
}
RationalNumber::RationalNumber(int num1, int num2)
{
	numer = num1;
	denom = num2;
}
RationalNumber::RationalNumber(int num1)
{
	numer = num1;
	denom = 1;
}
RationalNumber RationalNumber::add(RationalNumber& num)
{
	int num1 = 0, num2 = 0;
	int x = 0;
	x = num.denom;
	num1 = num.numer *denom;
	num2 = num.denom * denom;
	num1 += numer * x;
	RationalNumber added(num1, num2);

	return added;
}
RationalNumber RationalNumber::sub(RationalNumber num)
{
	int num1 = 0, num2 = 0;
	int x = 0, a = 0;

	x = num.denom;
	a = denom;
	numer = numer* x;
	denom = denom * x;
	num.numer = num.numer * a;
	num.denom = num.denom * a;
	num1 = numer - num.numer;
	num2 = x*a;

	RationalNumber subtracted(num1, num2);

	return subtracted;
}
RationalNumber RationalNumber::mul(RationalNumber num)
{
	int num1 = 0, num2 = 0;
	num1 = numer * num.numer;
	num2 = denom * num.denom;

	RationalNumber multiplied(num1, num2);

	return multiplied;
}
RationalNumber RationalNumber::div(RationalNumber num)
{
	int num1 = 0, num2 = 0;
	num1 = numer * num.denom;
	num2 = denom * num.numer;

	RationalNumber divided(num1, num2);

	return divided;
}
bool RationalNumber::equals(RationalNumber num)
{
	double a = 0, b = 0;
	a = numer / denom;
	b = num.numer / num.denom;
	if (a = b)
		return true;
	else
		return false;
}
bool  RationalNumber::less_than(RationalNumber num)
{
	double a = 0, b = 0;
	a = numer / denom;
	b = num.numer / num.denom;
	if (a < b)
		return true;
	else
		return false;
}
bool RationalNumber::greater_than(RationalNumber num)
{
	double a = 0, b = 0;
	a = numer / denom;
	b = num.numer / num.denom;
	if (a > b)
		return true;
	else
		return false;
}
RationalNumber RationalNumber::input(istream& in)
{
	cout << "For the Numerator: " << endl;
	in >> numer;
	cout << "and the Denomonator:" << endl;
	in >> denom;
	RationalNumber inputed(numer, denom);
	return inputed;
}
void RationalNumber::output(ostream& out)
{
	reduce();
	if (numer < 0 && denom < 0)
		out << numer *-1 << "/" << denom *-1 << endl;
	else if (numer > 0 && denom < 0)
		out << numer *-1 << "/" << denom *-1 << endl;
	else if (numer == 0)
		out << "Numer is zero, 0" << endl;
	else if (denom == 0)
		out << "Denom is zero, unavaliable" << endl;
	else
		out << numer << "/" << denom << endl;

}
int main()
{
	RationalNumber op1 = RationalNumber(1, -3);
	op1.output(cout);

	RationalNumber op2 = RationalNumber(2);
	op2.output(cout);

	RationalNumber op3 = RationalNumber(-2, -4);
	op3.output(cout);

	RationalNumber sum = op1.add(op2);
	cout << "sum op1 and 2 (5/3): ";
	sum.output(cout);


	RationalNumber difference = op2.sub(op3);
	cout << "difference op2 and 3 (3/2): ";
	difference.output(cout);

	RationalNumber product = op1.mul(op3);
	cout << "product op 1 3(-1/6): ";
	product.output(cout);

	RationalNumber quotient = op3.div(op2);
	cout << "quotient 3 and 2 1/4: ";
	quotient.output(cout);

	bool lessThan = op1.less_than(op2);
	bool greaterThan = op1.greater_than(op3);
	bool equalTo = op1.equals(RationalNumber(2, -6));
	bool equalTo2 = op1.equals(op2);

	cout << lessThan << endl;
	cout << greaterThan << endl;
	cout << equalTo << endl;
	cout << equalTo2 << endl;

}
You need to define gCD as part of RationalNumber:

1
2
3
4
int RationalNumber::gCD(int num1, int num2)
{
    // ...
}


And you should make it a static member of the class:
 
	static int gCD(int num1, int num2);


And in equals() you're using = where you mean ==.
Last edited on
Hi,

The compiler is your friend:

In member function 'bool RationalNumber::equals(RationalNumber)':
111:11: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
/tmp/ccCcnZ3o.o:
In function `RationalNumber::reduce()':
:(.text+0x1d): undefined reference to `RationalNumber::gCD(int, int)'
collect2: error: ld returned 1 exit status


If a function doesn't use any of the class members then it doesn't need to be in the class, so move the gCD function outside the class declaration.


= is for assignment == is for equality test

There are compound assignment operators:

numer /= r; //same as numer = numer / r;

http://en.cppreference.com/w/cpp/language/operator_assignment

Other things to do:

Get rid of using namespace std; it will bite you in the ass one day - Google it.

Split the class declaration into a header file - I like to use *.hpp for C++ headers ; put the definition of the functions into a cpp file. Name these files the same as the class name.
Last edited on
tpb you are an angel, I have been looking at the gCD function for so long just frustrated and then when it was the only syntax I did not know how to respond thank you, and TheIdeasMan, I am well aware of using namespace std; but for my computer science class my teacher told us not to get rid of it that we will start using new headers next year. Thanks for your help everyone.
Topic archived. No new replies allowed.