Case Study: Rational Numbers
In this section, I will discuss another popular example of operator function overloading: a class that encapsulates the implementation of rational numbers (exact fractions) and implements arithmetic operations and comparisons that are supported for integers.
Rational numbers can be represented as two components, the numerator and denominator. They allow the use of operations over fractions without rounding errors, e.g., 1/4 + 3/2 = 14/8 = 7/4.
In the class implementation, the numerator and denominator should be private data members. If the application might be ported to a 16-bit machine, the data members should be of type long. If it will run on 32-bit machines only, data members can be either int or long—on 32-bit ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access