Your implementation of the operator overloading is wrong. |
No, the OP's implementation is perfectly fine.
1 2
|
Fraction a, b, c;
c = a+b; // calls a.operator+(b)
|
In fraction.h, it's a really bad idea to have a
using
construct in a header file because it makes all those symbols visible to any other header files that you might include. Always put
using
in the cpp files and always after any
#include
s
Lines 96-98 of main.cpp: You declare variables of type
fractionType
but you have declared no such type. Do you mean
Fraction
?
You need to define the
<<
and
>>
operators for Fraction. nuderobmonkey has shown how to define
<<
. Use that to help define
>>
After the code is working and tested, save what you have and then consider this:
You could probably get some extra points by defining a normalize() method. Consider these problems with the class as written:
Fraction a(1,-2) create "1/-2" instead of "-1/2"
1/4 + 1/4 results in 2/4 instead of 1/2
1/2 / -1/2 will probably result in 2/-2 instead of -1/1
1/2 == 2/4 will probably return false
You can fix all of these problems by creating a function that casts a fraction into a standard form. It "normalizes" the fraction:
1 2 3 4
|
// Normalize a fraction
// Ensure that the denominator is positive.
// Reduce to lowest form by dividing numerator and denominator by their greatest common factor.
void Fraction::normalize();
|
At the end of each constructor and computation method, just call normalize() on the result.
This will force fractions to always be in normalized form. A side effect is that the comparison operations will work properly.
You will need this:
1 2
|
// Return the greatest common factor of two positive integers
int gcf(int a, int b);
|