Please note, that this question relates to an assignment for school.
We are building a custom Fraction class with most operators being overloaded. Most of them are not giving me problems. However, this part of the driver is not working for me:
cout << "testing post++ with f2 = f1++;" << '\n';
f2 = f1++;
cout << "f1 : " << f1 << '\n';
cout << "f2 : " << f2 << '\n';
assert(f1 == Fraction(6));
assert(f2 == Fraction(5));
cout << "ok\n\n";
What happens is that f2 gets assigned the value of f1++ and not pre-incremented f1, which is what the assert assumes should be there.
My operator looks like:
Fraction Fraction::operator++(int a)
{
numer = (numer + denom);
normalize();
return *this;
}
Now, I'm scratching my head over this because in my head the logic is that ++ has precedence over the assignment operator, so I'd have expected the asserts to test f1 and f2 with the same values for a post++ operation. For the ++pre overload, the assert values are equal to each other in the driver.
My question is, why should f2 take the pre-incremented value of f1, and how could I modify my operators to achieve this, or could this be an error from the prof?
Assignment operator:
Fraction& Fraction::operator=(const Fraction &rhs) {
numer = rhs.getNumer();
denom = rhs.getDenom();
return *this;
}