Download presentation
Presentation is loading. Please wait.
Published byLewis George Modified over 9 years ago
1
Operator Overloads Part2
2
Issue Provide member +(int) operator Rational + int OK int + Rational Error
3
Compiler View What compiler sees r2 + 4 r2.operator+(4) call + function on r2, pass it 4 4 + r2 4.operator+(r2) call + function on 4?!?!?
4
Overload Options Overload operator as member: r1 + r2 interpreted as r1.operator+(r2) Overload operator as non-member: r1 + r2 interpreted as +(r1, r2)
5
Overload Option 2 Provide operator as non-member function r1 + r2 interpreted as +(r1, r2) NOT a member. No Rational::
6
Option 2 Implementation Standard function r1 + r2 interpreted as +(r1, r2) – Need two parameters – No private access
7
Option 2 Advantage Standard function operator can place class last: Compiler view: 4 + r2 +(4, r2) Call + function with 4 and r2
8
Order matters 4 + r2 not the same as r2 + 4
9
Which Option? Implement operators as members or functions? No perfect answer… – Members : Access to private data First operand is implicit – must be your type – Standard function First operand can be other type Normally don't have access to privates…
10
Friends With Benefits Friend's of a class have access to its privates: OK if B is A's friend Private access not allowed
11
Friend + Can declare functions as friends: – Declares function – Makes friend of class – Gives private access:
12
Other Overloads Relational as non-members:
13
Relational Implementation Compare first to second, return true/false Nonmember implementation
14
Type Conversions
15
One arg constructors used for casts and implicit conversions:
16
+ int without operator With non-member function, compiler will implicitly construct objects to make operators work:
17
Type Conversions explicit keyword on constructors prevents implicit conversions
18
Type Conversions Conversion operators define how to cast away from Rational – Written as member function – Return type used as operator name
19
Conversions at Work: Any situation requiring primitive, compiler will use conversion operator:
20
Ambiguous Conversions Too many choices confuses compiler: Make things explicit:
21
[ ], >
22
LValues and RValues LValues : Things that can be on left side of = – Variables, other storage RValues : Everything else – Temporary values
23
BAD Subscript Can overload subscript operator – Easy access to member data
24
??? 2 = 3 r1 numerator2 denominator5
25
BAD Subscript Subscript supposed to produce Lvalue – returned int is not an Lvalue
26
GOOD Subscript Subscript must return a reference
27
r1.numerator = 3 r1 numerator2 denominator5
28
IO Operators Most classes should not do direct IO – No knowledge of context they will be used in toString() function better but clunky
29
IO Operators Most classes should not do direct IO – No knowledge of context they will be used in toString() function better but clunky Want to directly output/input:
30
IO Overloads Stream always left operand of insertion / extraction operators: Must overload > as friends
31
IO Overloads Overloading > – ostream means any output stream File, cout, stringstream… – istream any input stream – Return reference to the stream to allow chaining
32
IO Overloads Overloading > – ostream means any output stream File, cout, stringstream… – istream any input stream – Return reference to the stream to allow chaining cout
33
Output Stream insertion: – Pick info to send to stream – Return the stream (as reference) – Friend, so has private access
34
Input Stream extraction: – Read what you want from stream May discard some – Modify object passed as reference – Return the stream (as reference)
35
Input Stream extraction process: 12/30 rational numerator? denominator?
36
Input Stream extraction process: 12/30 rational numerator12 denominator?
37
Input Stream extraction process: 12/30 rational numerator12 denominator?
38
Input Stream extraction process: 12/30 rational numerator12 denominator30
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.