|
||
|
Lesson 2
Objective
|
Why overload operators? Understand why operator overloading is useful in C++. |
|
|
Just as a function name, such as print(), can be given a variety of meanings that depend on its arguments, so can an
operator be given additional meanings.
For example, the expression a + b has different meanings depending on the types of the variables a and b. Overloading the + operator for user-defined types allows them to be used in addition expressions, in much the same way as a native type. The expression a + b could mean string concatenation, complex number addition, or integer addition, depending on whether the variables were the ADT string, the ADT complex, or the native type int. Although meanings can be added to operators, their associativity and precedence remain the same. For example, the multiplication operator will remain of higher precedence than the addition operator. |
||
|
|
||