cdiaz851
I would add to dutch's response that this:
1 2 3 4
|
Rational::Rational()
{
Rational(0,1);
}
| |
Is not doing what you expect. What does happen you can see if you trace through that in a debugger.
Effectively, this creates a Rational inside the default constructor of a Rational, but it's temporary, and then evaporates when the default constructor finishes.
It doesn't set p and q of "this" Rational, it sets the p and q of the temporary, but that disappears.
dutch's code is the way of calling another constructor as a function (what you're calling as a function in your version is actually creating another temporary Rational), but it was added to the C++ language at some point (I don't remember when), and wasn't always legal code either (it's valid in modern C++).
Instead, if you can't modify the code per dutch, then call set in the default constructor.
Then, read your other constructor taking an int for the exact same problem, as that is doing this, too (creating a temporary Rational that disappears, leaving "this" Rational uninitialized).