Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
(BQ) Part 2 book "A complete guide to programming in C++" has contents: Overloading operators, dynamic memory allocation, dynamic members, inheritance, type conversion in class hierarchies, polymorphism, abstract classes,. and other contents. | chapter 19 Overloading Operators Overloading operators allows you to apply existing operators to objects of class type. For example, you can stipulate the effect of the + operator for the objects of a particular class. This chapter describes various uses of overloaded operators. Arithmetic operators, comparisons, the subscript operator, and the shift operators for input and output are overloaded to illustrate the appropriate techniques. The concept of friend functions, which is introduced in this context, is particularly important for overloading operators. 411 412 ■ CHAPTER 19 ■ OVERLOADING OPERATORS GENERALS Overloadable operators Operators + == - Meaning * / ++ -- != >= Logical operators ! Assignment operators (op is a binary arithmetic or a binary bitwise operator) = op= & | ^ () ~ , new delete ✓ Relational operators >> Bitwise operators Function call, subscript operator Other operators NOTE The assignment operator =, the address operator &, and the comma operator, have a predefined meaning for each built-in type. This meaning can be changed for classes by a definition of your own. ᮀ Rules An operator is always overloaded in conjunction with a class. The definition scope of an operator is simply extended—the characteristics of the operator remain unchanged. The following rules apply: ■ ■ ■ ■ You cannot create “new operators”—that is, you can only overload existing operators. You cannot redefine the operators for fundamental types. You cannot change the operands of an operator. A binary operator will always be binary and a unary operator will always be unary. The precedence and the order of grouping operators of the same precedence remains unchanged. GENERALS ■ 413 ᮀ Overloading An operator is said to be overloaded if it is defined for multiple types. In other words, overloading an operator means making the operator significant for a new type. Most operators are already overloaded for fundamental types. In the case of .