9
Function and Operator Overloading

Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler

Embed Size (px)

Citation preview

Page 1: Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler

Function and Operator Overloading

Page 2: Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler

Overloading

• Review of function overloading– It is giving several definitions to a single function

name

– The compiler can figure out which definition to use by the number and type of arguments

• Operator overloading is giving additional definitions to operators

• Some operators that can be overloaded +, -, >, <, [ ], *, etc

Page 3: Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler

The difference between a function and an operator• An operator (such as +) is really just a function

with different syntax• In a function, the argument are placed in

parentheses after the function name, as in:– add(cost, tax)

– This is postfix notation

• For an operator, the argument are placed on either side of the operator, as in:– cost + tax

– This is infix notation

Page 4: Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler

Advantages of operator overloading• The point of operator overloading is to

provide the same concise expression for user-defined types as built in types

• It is just clearer and simpler to say– Cost + tax, than to say– Add(cost, tax)

Page 5: Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler

The role of types in overloading

• C++ programming is a type sensitive and type-focused process

• Programmers can use built-in types or define their own types

• Operators can be used with user-defined types as well as built-in types– The operators new meaning must be defined

• The compiler uses the type of the arguments to decide which definition to use for overloaded functions or operators

Page 6: Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler

How operators are overloading

• You write a function (with a header and body) as you normally would

• The function name is now the keyword operator followed by the symbol to be overloaded– Example: return_type operator++( )

• To use an operator on a class object, that operator must be overloaded (with a couple of exceptions)– The assignment operator (=)

– The address operator (&)

Page 7: Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler

Restrictions on operator overloading

• The precedence of an operator cannot be changed by overloading.– An aside on precedence—table on precedence

• You cannot change the number of operands(arguments) an operators takes– Unary operators remain unary; binary remain

binary; the only ternary operator (:?) cannot be overloaded

• It is not possible to create new operators– So ** for exponentiation cannot be created

Page 8: Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler

Operator functions as class members vs. as friend functions

• They can be either class members or friends• As member functions:

– The leftmost (or only) argument must be a class object or reference to a class object

• If the left operand must be a built-in type or object of a different class, it must be a non-member function– If it must access private members of the class directly,

then it must be a friend function

Page 9: Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler

Data Conversions

• Type conversions between built-in data types may be implicit or explicit– Use static_cast<new_type>(old_type_variable) for

explicit type conversions

• All conversions between objects and built-in types must be explicit

• Example: convert 5 feet 3 inches to 5.25 feet this is a conversion from Distance to a float