C++ programming is a type-sensitive and type-focused process. Operators provide programmers with a concise notation for expressing manipulations of objects of built-in types.
- 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 +
- 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 = object1.add(object2);
- object2 = object2 + object1;
Overloading is especially appropriate for mathematical classes. These often require that a substantial set of operators be overloaded to ensure consistency with the way these mathematical classes are handled in the real world. Operator overloading is not automatic, however; the programmer must write operator-overloading functions to perform the desired operations. Sometimes these functions are best made member functions; sometimes they are best as friend functions; occasionally the can be made non-member, non-friend functions.
2004-07-29