Đang chuẩn bị liên kết để tải về tài liệu:
Chapter 8 Operator Overloading, Friends, and References
Không đóng trình duyệt đến khi xuất hiện nút TẢI XUỐNG
Tải xuống
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 e.g., +, -, = , %, ==, /, * 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 . | 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 e.g., +, -, = , %, ==, /, * 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 Basics Overloading operators VERY similar to overloading functions Operator itself is "name" of function Example Declaration: const Money operator +( const Money& amount1, const Money& amount2); Overloads + for operands of type Money Uses constant reference parameters for efficiency Returned value is type Money Allows addition of "Money" objects Overloaded "+" 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 Just perform "addition" particular to "your" type Money "+" Definition: Display 8.1 Operator Overloading Definition of "+" operator for Money class: Overloaded "==" Equality operator, == Enables comparison of Money objects Declaration: bool operator ==(const Money& amount1, const Money& amount2); Returns bool type for true/false equality Again, it’s a non-member function (like "+" overload)