tailieunhanh - Chương 8 - Operator Overloading
Overloading unary operators Non-static member function, no arguments. Non-member function, one argument Argument must be class object or reference to class object Remember, static functions only access static data | Chapter 8 - Operator Overloading Outline Introduction Fundamentals of Operator Overloading Restrictions on Operator Overloading Operator Functions as Class Members vs. as friend Functions Overloading Stream-Insertion and Stream-Extraction Operators Overloading Unary Operators Overloading Binary Operators Case Study: Array Class Converting between Types Case Study: A String Class Overloading ++ and -- Case Study: A Date Class Standard Library Classes string and vector Introduction Use operators with objects (operator overloading) Clearer than function calls for certain classes Operator sensitive to context Examples Fundamentals of Operator Overloading Types Built in (int, char) or user-defined Can use existing operators with user-defined types Cannot create new . | Chapter 8 - Operator Overloading Outline Introduction Fundamentals of Operator Overloading Restrictions on Operator Overloading Operator Functions as Class Members vs. as friend Functions Overloading Stream-Insertion and Stream-Extraction Operators Overloading Unary Operators Overloading Binary Operators Case Study: Array Class Converting between Types Case Study: A String Class Overloading ++ and -- Case Study: A Date Class Standard Library Classes string and vector Introduction Use operators with objects (operator overloading) Clearer than function calls for certain classes Operator sensitive to context Examples Fundamentals of Operator Overloading Types Built in (int, char) or user-defined Can use existing operators with user-defined types Cannot create new operators Overloading operators Create a function for the class Name function operator followed by symbol Operator+ for the addition operator + Fundamentals of Operator Overloading Using operators on a class object It must be overloaded for that class Exceptions: Assignment operator, = Memberwise assignment between objects Address operator, & Returns address of object Both can be overloaded Overloading provides concise notation object2 = (object2); object2 = object2 + object1; Restrictions on Operator Overloading Cannot change How operators act on built-in data types ., cannot change integer addition Precedence of operator (order of evaluation) Use parentheses to force order-of-operations Associativity (left-to-right or right-to-left) Number of operands & is unitary, only acts on one operand Cannot create new operators Operators must be overloaded explicitly Overloading + does not overload += Restrictions on Operator Overloading Operator .
đang nạp các trang xem trước