Classes problem

Im working on a class with fractions. I have a method called plus that takes another fraction as an argument and returns the sum of the receiver and the argument. For example, if r = 2/3 and s = 3/4, then r.plus(s) returns another fraction object whose value is 17/12. Note that both the receiver and argument are left unchanged. The returned fraction is not reduced.

I have
int plus( const Fraction & f2 ) const { return ( ( b * f2.a ) + ( a * f2.b ) ) << '/' << ( b * f2.b ); }

I am getting the warning in the title.

I think my problem is the << '/' << in the return part.
But without that im not sure how to return a fraction that would look like 17/12

here is some of the code
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
#include <iostream>
#include <fstream>

using namespace std;

class Fraction
{
public:
	Fraction() { a = 0; b = 1; }
	Fraction( int num ) { a = num; b = 1; }
	Fraction( int num1, int num2 ) { a = num1; b = num2; }

	void read( istream & in );
	void print( ostream & out ) { out << a << '/' << b; }
	void printMixed( ostream & out );
	int plus( const Fraction & f2 ) const { return ( ( b * f2.a ) + ( a * f2.b ) ) << '/' << ( b * f2.b ); }
	int times( const Fraction & f2 ) const { return ( a * f2.a ) << '/' << ( b * f2.b ); }

	void setFraction( int num1, int num2 ) { Fraction( num1, num2 ); }
	int getNumerator() const { return a; }
	int getDenominator() const { return b; }

	friend int operator+( const Fraction & f1, const Fraction & f2 );
	friend int operator*( const Fraction & f1, const Fraction & f2 );
	friend bool operator<( const Fraction & f1, const Fraction & f2 );
	friend bool operator==( const Fraction & f1, const Fraction & f2 );

private:
	int a, b, n;
};


Lines 16 and 17 both have the problem
Last edited on
Well you are returning an int, which doesn't happen to include '/'s
What you may wish to do is change your return type to a string and compose the string (using osstringstream) when you return it.
or you could make a new class fraction with a numerator and denominator

edit: oh you may want to revise your title, if you can.
Last edited on
Ideally, your plus function should return a new instance of your fraction class:

 
Fraction plus(const Fraction&) const;


Same with your operator overload:
 
friend const Fraction operator+(const Fraction&, const Fraction&);


Notice the const instance return of the operator. This is to guard against silliness like:
1
2
Fraction a, b, c;
(a + b) = c;


Props on the const correctness, though! I rarely see people new to C++ use const and & in their classes. I didn't use them much either when I was new. But, when I got to really writing a LOT of code, they started to naturally make sense.
I see what you mean by that.

So should Plus create a new fraction object, then just have it return the new object.

Something like this:
1
2
3
4
5
Fraction Fraction::plus( const Fraction & f2 ) const
{
	Fraction f1( ( b * f2.a ) + ( a * f2.b ), ( b * f2.b ) );
	return f1;
}
Last edited on
Well I tried that and it worked.

Thank you sadavied.
Topic archived. No new replies allowed.