Rational class problems

I'm working on this problem: Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use Integer values to represent the private data of the class-the numerator and the denominator. Provide a constructor should contain default values in case no initializes are provided and should store the fraction in reduced form.
provide public member functions that perform each of the following tasks:
adding, subtracting, multiplying dividing, two rational numbers

this is what i have so far
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

// header file

  #ifndef RATIONAL_H_
#define RATIONAL_H_


//Rational class definition

class Rational {
public:
	Rational();
	Rational ( int n = 0, int d = 1); //default case
	void adding( const Rational &);  // a) adding two rational numbers
	void subtracting(const Rational &); // b) subtracting two rational numbers
	void multiplying(const Rational &);  // c) multiplying two rational numbers
	void dividing(const Rational &);    // d) dividing two rational numbers
	void printRational();   // e) printing in form of a/b, where a is numerator and b is denominator
	void printFloat();    // f) printing in floating point format

private:
	int numerator;
	int denominator;
	void reduce();
};
#endif /* RATIONAL_H_ */




//source file

#include <iostream>
#include <iomanip>
#include "Rational.h"
using namespace std;


 Rational::Rational(int n, int d) {
	numerator = d<0 ? -n:n;   // to make sure that if a negative number is entered, it will be in the numerator
	denominator = d<0 ? -d:n;
	reduce();
}

Rational Rational::adding (const Rational& a) {
	Rational r;

	r.numerator = a.numerator * denominator + a.denominator * numerator;
	r.numerator = a.denominator * denominator;
	r.reduce();

	return r;
}

Rational Rational::subtracting (const Rational& s) {
	Rational r;

	r.numerator = s.numerator * denominator - s.denominator * numerator;
	r.numerator = s.denominator * denominator;
	r.reduce();

	return r;
}

Rational Rational::multiplying (const Rational& m){
	Rational r;

	r.numerator = m.numerator * numerator;
	r.numerator = m.denominator * denominator;
	if (denominator != 0)
	r.reduce();

	return r;
}

Rational Rational::dividing (const Rational& b){
	Rational r;

	r.numerator = b.denominator * numerator;
	r.numerator = b.numerator * denominator;
	if (denominator != 0)
	r.reduce();

	return r;

}

void Rational::printRational(){
	if (denominator = 0)
		cout<< "Error" <<endl;
	else{
		cout << numerator << "/" << denominator << endl;
	}
}

void Rational::printFloat(){

	if (denominator = 0)
		cout << "Error" <<endl;
	else
		cout << numerator / denominator << "." << numerator%denominator <<endl; //float(numerator) float(denominator) ????????


}

void Rational::reduce(){
	int n = numerator < 0 ? -numerator : numerator;
	int d = denominator;
	int largest = n > d ? n : d;

	int gcd = 0;

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




// program to test class
#include <iostream>
#include "Rational.h"
using namespace std;

int main(){
	Rational r;

	cout<< "Please enter a numerator: " << endl;
	cin >> r.numerator;

	cout<< "Please enter a denominator: " << endl;
	cin >> r.denominator;

	cout << "The rational number you entered is: " << r.printRational() << endl;

	cout << "The floating point format of the rational number you entered is: " << r.printFloat() << endl;


	//adding
	cout<< "Please enter another rational number to add to your original number. Enter the numerator first, then the denominator: "<<endl;
	cin >> r.numerator >>r.denominator;
	 // How would i call the functions under adding that i declared in Rational.cpp??


}






I am lost on how I should integrate everything in the testing class. How do I call the functions under the adding, subtracting etc. that I declared in Rational.cpp? Any help would be appreciated since I'm still very new to C++ and I'm having a hard time grasping this topic
1
2
3
4
5
6
Rational
   a(4,2),
   b(6,9),
   c = a.dividing(b);

   c.printRational();
Compare lines 14 and 45. Do you notice a mismatch?


Based on lines 141 and 143 you do know how to call a member function. The "addition" is
a member function. It takes a parameter.
1
2
3
Rational foo;
Rational bar;
Rational gaz = foo.addition( bar );

Alas! According to line 14 the "addition" does not return a value?
thanks for the input, I changed the code up a little and now the completed code looks like this.

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
/*
 * Rational.h
 *
 *  Created on: Nov 16, 2016
 *      
 *      header file for Rational class
 */

#ifndef RATIONAL_H_
#define RATIONAL_H_


//Rational class definition

class Rational {
public:
	Rational();
	Rational ( int n = 0, int d = 1); //default case
	 int adding( const Rational& a);  // a) adding two rational numbers
	 int subtracting(const Rational& s); // b) subtracting two rational numbers
	 int multiplying(const Rational& m);  // c) multiplying two rational numbers
	int  dividing(const Rational& b);    // d) dividing two rational numbers
	void printRational();   // e) printing in form of a/b, where a is numerator and b is denominator
	void printFloat();    // f) printing in floating point format

private:
	int numerator;
	int denominator;
	void reduce();
};
#endif /* RATIONAL_H_ */


/*
 * Rational.cpp
 *
 *  Created on: Nov 16, 2016
 *      
 */

#include <iostream>
#include <iomanip>
#include "Rational.h"
using namespace std;


 Rational::Rational(int n, int d) {
	numerator = d<0 ? -n:n;   // to make sure that if a negative number is entered, it will be in the numerator
	denominator = d<0 ? -d:n;
	reduce();
}
 Rational Rational::adding ( const Rational& a) {
	Rational r;

	r.numerator = a.numerator * denominator + a.denominator * numerator;
	r.numerator = a.denominator * denominator;
	r.reduce();

	return r;
}

Rational Rational::subtracting (const Rational& s) {
	Rational r;

	r.numerator = s.numerator * denominator - s.denominator * numerator;
	r.numerator = s.denominator * denominator;
	r.reduce();

	return r;
}

Rational Rational::multiplying (const Rational& m){
	Rational r;

	r.numerator = m.numerator * numerator;
	r.numerator = m.denominator * denominator;
	if (denominator != 0)
	r.reduce();

	return r;
}

 Rational Rational::dividing (const Rational& b){
	Rational r;

	r.numerator = b.denominator * numerator;
	r.numerator = b.numerator * denominator;
	if (denominator != 0)
	r.reduce();

	return r;

}

void Rational::printRational(){
	if (denominator = 0)
		cout<< "Error" <<endl;
	else{
		cout << numerator << "/" << denominator << endl;
	}
}

 void Rational::printFloat(){

	if (denominator = 0)
		cout << "Error" <<endl;
	else
		cout << numerator / denominator << "." << numerator%denominator <<endl; //float(numerator) float(denominator) ????????


}

void Rational::reduce(){
	int n = numerator < 0 ? -numerator : numerator;
	int d = denominator;
	int largest = n > d ? n : d;

	int gcd = 0;

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



/*
 * assign8.cpp
 *
 *  Created on: Nov 16, 2016
 *      
 *
 *      program to test Rational class
 *
 *      ex. 9.15
 */

#include <iostream>
#include "Rational.h"
#include "Rational.cpp"
using namespace std;

int main(){
	Rational x(-3/6), y(-25/40), z, w(9);

//adding
	x.printRational();
	cout<< " + ";
	y.printRational();
	z = x.adding(y);
	cout<< " = ";
	z.printRational();
	cout << endl;
	z.printRational();
	cout<< " = ";
	z.printFloat();
	cout << endl;

// subtracting
	x.printRational();
		cout<< " - ";
		y.printRational();
		z = x.subtracting(y);
		cout<< " = ";
		z.printRational();
		cout << endl;
		z.printRational();
		cout<< " = ";
		z.printFloat();
		cout << endl;

// multiplying
		x.printRational();
			cout<< " * ";
			y.printRational();
			z = x.multiplying(y);
			cout<< " = ";
			z.printRational();
			cout << endl;
			z.printRational();
			cout<< " = ";
			z.printFloat();
			cout << endl;

//dividing
		x.printRational();
			cout<< " / ";
			y.printRational();
			z = x.adding(y);
			cout<< " = ";
			z.printRational();
			cout << endl;
			z.printRational();
			cout<< " = ";
			z.printFloat();
			cout << endl;
}



Does anyone see any errors in this? I am using eclipse and it won't compile. it is showing me some errors on lines 19-22 ( candidate is: ( whatever the line has written for ex int Rational::adding (const Rational& )) and on lines 52, 62, 72, 83 it's saying it doesn't match but i thought it did in this new code???
Last edited on
What type of value should the function "adding" return? Nothing (i.e. void)? An int? A Rational?
Topic archived. No new replies allowed.