TAILIEUCHUNG - Chapter 8 Operator Overloading, Friends, and References
Given previous example: Note: overloaded "+" NOT member function Definition is "more involved" than simple "add", Requires issues of money type addition. Must handle negative/positive values Operator overload definitions generally very simple | Chapter 8 Operator Overloading, Friends, and References Learning Objectives Basic Operator Overloading Unary operators As member functions Friends and Automatic Type Conversion Friend functions, friend classes Constructors for automatic type conversion References and More Overloading > Operators: = , [], ++, -- Operator Overloading Introduction Operators +, -, %, ==, etc. Really just functions! Simply "called" with different syntax: x + 7 "+" is binary operator with x & 7 as operands We "like" this notation as humans Think of it as: +(x, 7) "+" is the function name x, 7 are the arguments Function "+" returns "sum" of it’s arguments Operator Overloading Perspective Built-in operators ., +, -, = , %, ==, /, * Already work for C++ built-in types In standard "binary" notation We can overload them! To work with OUR types! To add "Chair types", or "Money types" As appropriate for our needs In "notation" we’re comfortable with Always overload with similar "actions"! Overloading .
đang nạp các trang xem trước