Operator Overloads Part1
Rational Class RationalNumber (Fraction) class: Rational -numerator: int -denominator: int +Rational(numerator: int, denominator: int) +getNumerator(): int const +getDenominator(): int const +add(secondRational: Rational&): Rational const +subtract(secondRational: Rational&): Rational const +multiply(secondRational: Rational&): Rational const +divide(secondRational: Rational&): Rational const +compareTo(secondRational: Rational&): int const +equals(secondRational: Rational&): bool const +intValue(): int const +doubleValue(): double const +toString(): string const -gcd(n: int, d: int): int
Rational Use Currently awkward to use:
Rational Use What we want:
Operator Overloading Classes get = and . operators automatically May need to change = Can overload other operators + - / * == > etc… Can't Make new operators Change precedence Change how operators work on plain types
Overload as Member Overload as member function: r1 + r2 interpreted as r1.operator+(r2)
Overload as Member Implemented as member function:
Notes Parameter can be any type: Return type doesn't have to be same class:
Other Overloads Other math as members:
Other Overloads Relational as members:
Relational Implementation Compare this to other, return true or false
Unary Unary operators
Augmented Assignments Expected to change original Expected to return a reference to original object
Augmented Assignment Augmented assignments return current object as reference
Augmented Assignment Augmented assignments return current object as reference r3 numerator 1 denominator 3
Augmented Assignment Augmented assignments return current object as reference r3 numerator 11 denominator 15
Augmented Assignment Augmented assignments return current object as reference r3 r3 numerator 11 denominator 15
Augmented Assignment Augmented assignments return current object as reference r3 r3 numerator 63 denominator 45
Augmented Assignment Augmented assignments return current object as reference r3 r3 numerator 63 denominator 45
++i vs i++ Preincrement Postincrement int x = 10; int y = ++x; x = 11 Change x, use new value Copy x to use in rest of statement, change x
Pre-crements Preincrement/decrement Operator ++ or -- Return reference to original
Pre behavior Preincrement in action: r2 numerator 2 denominator 3
Pre behavior Preincrement in action: r2 numerator 5 denominator 3
Pre behavior Preincrement in action: r2 r2 numerator 5 denominator 3 temp numerator 5 denominator 3
Post-crements Postincrement/decrement Dummy int param to distinguish pre/post Don't use Return a new object with old value
Post behavior Postincrement in action: r2 numerator 2 denominator 3
Post behavior Postincrement in action: r2 numerator 2 denominator 3 temp numerator 2 denominator 3
Post behavior Postincrement in action: r2 numerator 5 denominator 3 temp numerator 2 denominator 3
Post behavior Postincrement in action: temp r2 numerator 5 denominator 3 temp2 numerator 2 denominator 3 temp numerator 2 denominator 3
Post behavior Postincrement in action: r2 numerator 5 denominator 3 temp2 numerator 2 denominator 3
Important Lesson x++ generally involves copy for objects Favor ++x over x++ for objects