Download presentation
Presentation is loading. Please wait.
1
Operator Overloads Part1
2
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
3
Rational Use Currently awkward to use:
4
Rational Use What we want:
5
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
6
Overload as Member Overload as member function:
r1 + r2 interpreted as r1.operator+(r2)
7
Overload as Member Implemented as member function:
8
Notes Parameter can be any type:
Return type doesn't have to be same class:
9
Other Overloads Other math as members:
10
Other Overloads Relational as members:
11
Relational Implementation
Compare this to other, return true or false
12
Unary Unary operators
13
Augmented Assignments
Expected to change original Expected to return a reference to original object
14
Augmented Assignment Augmented assignments return current object as reference
15
Augmented Assignment Augmented assignments return current object as reference r3 numerator 1 denominator 3
16
Augmented Assignment Augmented assignments return current object as reference r3 numerator 11 denominator 15
17
Augmented Assignment Augmented assignments return current object as reference r3 r3 numerator 11 denominator 15
18
Augmented Assignment Augmented assignments return current object as reference r3 r3 numerator 63 denominator 45
19
Augmented Assignment Augmented assignments return current object as reference r3 r3 numerator 63 denominator 45
20
++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
21
Pre-crements Preincrement/decrement Operator ++ or --
Return reference to original
22
Pre behavior Preincrement in action: r2 numerator 2 denominator 3
23
Pre behavior Preincrement in action: r2 numerator 5 denominator 3
24
Pre behavior Preincrement in action: r2 r2 numerator 5 denominator 3
temp numerator 5 denominator 3
25
Post-crements Postincrement/decrement
Dummy int param to distinguish pre/post Don't use Return a new object with old value
26
Post behavior Postincrement in action: r2 numerator 2 denominator 3
27
Post behavior Postincrement in action: r2 numerator 2 denominator 3
temp numerator 2 denominator 3
28
Post behavior Postincrement in action: r2 numerator 5 denominator 3
temp numerator 2 denominator 3
29
Post behavior Postincrement in action: temp r2 numerator 5 denominator
3 temp2 numerator 2 denominator 3 temp numerator 2 denominator 3
30
Post behavior Postincrement in action: r2 numerator 5 denominator 3
temp2 numerator 2 denominator 3
31
Important Lesson x++ generally involves copy for objects
Favor ++x over x++ for objects
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.