Session_11 - Lesson 13 Operator Overloading(modified For...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Lesson 13 – Operator Overloading (modified) For this chapter we are going to use different examples than the textbook. Some operators in C++ have multiple meanings. The stream insertion operator << can also be used as the left shift operator as shown below: #include <iostream> using namespace std; int main( void ) { int i = 1; cout << "i = " << i << " And i shifted left is " ; i <<= 1; cout << i << endl; return 0; } Similarly >> can be overloaded. Both of these operators are overloaded in the C++ class library. The C++ language itself overloads + and -; these operators perform differently depending on their context in integer, floating point and pointer arithmetic. C++ does not allow new operators to be created, but it does allow most existing operators to be overloaded so that when these operators are used with class objects, the operators have meaning appropriate to the new types. Operators are overloaded by writing a function definition (with a header and body) as you normally would, except that the function name now becomes the keyword operator followed by the symbol for the operator being overloaded. For example, the function name operator+ would be used to overload the addition operator (+). To use an operator on class objects, that operator must be overloaded—with two exceptions. The assignment operator (=) may be used with every class without explicit overloading. The default behavior of the assignment operator is a member wise assignment of the data members of the class. Also, the address operator (&) may also be used with objects of any class without overloading; it simply returns the address of the object in memory. The address operator can also be overloaded. Operator overloading is not automatic; 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 (explained later), and occasionally they can be made non-member, non-friend functions. Most of C++’s operators can be overloaded. It’s easier to show the ones that cannot: . .* :: ?: sizeof Page 1 of 9
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
When you overload an operator, you cannot change the number of operands an operator takes. Overloaded unary operators remain as unary operators; overloaded binary operators remain as binary operators. C++’s only ternary operator (?:) cannot be overloaded. Operators &, *, + and – each have unary and binary versions; these unary and binary versions can be overloaded separately. It is not possible to create new operators; only existing operators can be overloaded. Operator overloading works only with objects of user-defined types or with a mixture of an object of a user-defined type and an object of a built-in type. Overloading an assignment operator and an addition operator to allow statements like
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 9

Session_11 - Lesson 13 Operator Overloading(modified For...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online