Step 9 of 9
Challenge: a Fraction class
A value type that always keeps itself in a clean form is a great exercise in invariants. Your turn: write class Fraction whose invariant is: always fully reduced, denominator always positive.
Fraction(int num, int den = 1):Fraction(2, -4)becomes-1/2. You can assumeden != 0.int num() const,int den() constFraction plus(const Fraction& o) constandFraction times(const Fraction& o) conststd::string str() const:"3/4", or just"2"when the denominator is 1
std::gcd(a, b) from <numeric> gives the greatest common divisor (it handles negative numbers).