const int GetDen(void) { return _den; }
should (or maybe)
int GetDen(void) const { return _den; }
The first returns a copy of const. Returning by copy means that the copy will be generated and provided to the caller's function as a temporary object, so the const object is a read-only object. Thus, returning a copy makes a read-only copy.
The second makes your function callable even if the const fraction object is an accessible read-only function (you can tell). This is probably what you wanted to write, and if not, then better than without const.
By the way, the void parameter in the parameters is not useful in C ++, you can not print it and just say int GetNum() const{ return _num; } int GetNum() const{ return _num; }
Another detail: names starting with an underscore are reserved by the standard for standard implementations. It is not very dangerous to use, but you never know. If you want to keep them, at least encapsulate all your code in a namespace.
Klaim
source share