Overloading operators - Questions

Nov 16, 2010 at 11:26pm
Ok, so in class our newest program assignment is to overload +, -, *, /, =, and << operators.


I've written a class that handles Fractions and right now I am only trying to overload the + operator and the << operator. I figured once I accomplished this the rest would fall out easy.

I'm not sure what I've done wrong and I'm not the greatest are debugging and working in classes makes it that much more difficult. I am looking for a little direction here. If anyone could possible check my code and point in the right direction I would be truly thankful!

Header File:
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
class Fractions
{
public:

	Fractions();
	Fractions(int num, int denom);
	void setNumerator(int num);
	void setDenominator(int denom);
	int getNumerator();
	int getDenominator();
	void displayFraction();
	Fractions& operator+(Fractions p);
	friend ostream& operator<<(ostream &out, Fractions &p)
	{
		out << p.numerator << "/" << p.denominator << endl;

		return out;
	}

private:

	int numerator;
	int denominator;
	
};


Implementation File:
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
#include "Fractions.h"
#include <iostream>

using namespace std;

//************************************************************************
Fractions::Fractions()
{
	numerator = 0;
	denominator = 0;
	
}
//************************************************************************

//************************************************************************
Fractions::Fractions(int num, int denom)
{
	numerator = num;
	denominator = denom;
}
//************************************************************************

//************************************************************************
void Fractions::setNumerator(int num)
{
	numerator = num;
}
//************************************************************************

//************************************************************************
void Fractions::setDenominator(int denom)
{
	denominator = denom;
}
//************************************************************************

//************************************************************************
int Fractions::getNumerator()
{
	return numerator;
}
//************************************************************************
int Fractions::getDenominator()
{
	return denominator;
}
//************************************************************************

//************************************************************************
void Fractions::displayFraction()
{
	cout << "Fraction:  " << getNumerator() << "/" << getDenominator() << endl;
}
//************************************************************************

//************************************************************************
Fractions& Fractions::operator+(Fractions p)
{
	Fractions retfrac;

	retfrac.numerator = (numerator * p.denominator) + (p.numerator * denominator);
	retfrac.denominator = denominator + p.denominator;

	
	return retfrac;
}
//************************************************************************ 


Main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "Fractions.h"             //Header File for Class Fractions

using namespace std;

int main()
{
	Fractions F1;                
	Fractions F2(3, 4);           

	cout << "Added: " <<  F1 + F2 << endl;


	F1.displayFraction();
	F2.displayFraction();

	
	system("PAUSE");
	return 0;
}



I've run this class without the overloading and it works, and I've edited my code a lot since then.

Thanks,

ChrisC
Last edited on Nov 16, 2010 at 11:27pm
Nov 16, 2010 at 11:29pm
Line 12 in the first file: Fractions& operator+(Fractions p);

This should take a const Fractions reference, not copy it.

Other then that...I don't see anything incorrect.
Nov 16, 2010 at 11:36pm
1
2
3
4
5
6
7
c:\documents and settings\family\my documents\visual studio 2010\projects\fractions\fractions\fractions.h(22): error C2143: syntax error : missing ';' before '&'
c:\documents and settings\family\my documents\visual studio 2010\projects\fractions\fractions\fractions.h(22): error C2433: 'ostream' : 'friend' not permitted on data declarations
c:\documents and settings\family\my documents\visual studio 2010\projects\fractions\fractions\fractions.h(22): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\documents and settings\family\my documents\visual studio 2010\projects\fractions\fractions\fractions.h(22): error C2061: syntax error : identifier 'ostream'
c:\documents and settings\family\my documents\visual studio 2010\projects\fractions\fractions\fractions.h(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\documents and settings\family\my documents\visual studio 2010\projects\fractions\fractions\fractions.h(23): error C2805: binary 'operator <<' has too few parameters
c:\documents and settings\family\my documents\visual studio 2010\projects\fractions\fractions\fractions.h(23): error C2333: 'Fractions::operator <<' : error in function declaration; skipping function body



It's giving me these errors associated with my << operator overload in the header. Any ideas?
Nov 16, 2010 at 11:37pm
That line should also return a Fractions and not a Fractions&. Since you are returning a reference to a local object, which is bad.


Also, since the class only represents a single fraction and not multiple fractions, Fraction would be a much more sensible name.

EDIT:

Also, it should be std::ostream& and not ostream.

including <iostream> at the top of Fractions.h is probably a good idea, too.
Last edited on Nov 16, 2010 at 11:38pm
Nov 16, 2010 at 11:46pm
Heck Ya! You guys are awesome. Thanks for the help. I got it running so know I can go back and overload the others.

Also, Disch, right now you are correct I am only handling one fraction for simplicity. My goal is to be able to have the user input multiple fractions to be added, subtracted, multiplied, divided, and checked to see if they equal each other or not.

Again, guys thanks for the help! I will try and post the completed code soon.


Thanks,

ChrisC
Nov 17, 2010 at 12:04am
Also, Disch, right now you are correct I am only handling one fraction for simplicity. My goal is to be able to have the user input multiple fractions to be added, subtracted, multiplied, divided, and checked to see if they equal each other or not.


Right... but the class itself will only be one of those fractions. If the user enters mulitple fractions, each fraction will have it's own object.

1
2
Fractions a;  // here's one fraction
Fractions b;  // here's another one 


a and b are both a "Fractions" -- but they each only represent a singular fraction.

Do you see what I mean? The class itself represents one fraction, so it should be singular Fraction.
Nov 17, 2010 at 12:52am
Yep, I see where you're coming from now. Was on a different page I guess.
Topic archived. No new replies allowed.