[1st year University Comp Sci] Struggling with adding fractions in a sequence

I'm not sure if I am misunderstanding or not, but I've been tasked with creating a function that adds fractions to the nth term. Though I'm not exactly sure how to do that without using a mutator. If I understand correctly. I must pass the add function the Rational h for it to properly work, would I just pass it Rational h and then completely ignore Rational h as I do not want to mutate it?
What exactly am I to do with my result? Run it directly into a print function from my add function?


rational_test.cpp
1
2
3
4
5
6
7
8
9
Rational h;
	int n;

	cout << "Now runnning rational.cpp" << endl;

	cout << "The harmonic series is the following infinite series: '1 + 1/2 + 1/3 + ...'" << endl;
	cout << "Finding H(10) of the series" << endl;
	n = 10;
	h.add(n);//Run function add to find H(10) 


rational.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
class Rational
{
private:
	long int numerator;
	long int denominator;
public:
	Rational(); // Default constructor
	Rational(const long int, const long int);

//
// add
//
//Purpose- An arithmetic addition operation that adds one instance of Rational to another
//
//Parameter(s)- 
//              n: the number of terms that will be added
//Precondition(s)- N/A
//
//Returns-
//
//Side Effect-
//
//
	void add(const int) const;


rational.cpp
1
2
3
4
5
6
//NONE of the member functions should be mutation operation.
//Declare all member functions to be const member functions.
	void Rational::add(const int n) const
	{
		
	}
You could return a new Rational.
Rational Rational::add(const int n) const
{
}
1
2
3
4
5
6
// An arithmetic addition operation that adds one instance of Rational to another

Rational Rational::add(const Rational &r) const
{

}



Or you could use a non-member function instead (provided you dealt with the private/public access):
1
2
3
4
Rational operator + ( const Rational &r, const Rational &s )
{

}
Last edited on
Topic archived. No new replies allowed.