tailieunhanh - 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 . | 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 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 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)

crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.