Operator Overloading

21
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. Operator Overloading is useful for redefining built-in operations for user defined types. Operator Overloading should be used to perform the same function or similar function on class objects as the built-in behavior.

description

Operator Overloading. Operator Overloading allows a programmer to define new types from the built-in types. Operator Overloading is useful for redefining built-in operations for user defined types. - PowerPoint PPT Presentation

Transcript of Operator Overloading

Page 1: Operator Overloading

Operator Overloading

• Operator Overloading allows a programmer to define new types from the built-in types.– Operator Overloading is useful for redefining

built-in operations for user defined types.– Operator Overloading should be used to

perform the same function or similar function on class objects as the built-in behavior.

Page 2: Operator Overloading

Operator Overloading

• An operator is overloaded by writing a function definition that has a function name of operator followed by the symbol for the operator to be overloaded.operator- will define a function that overrides

the built-in subtraction operator.

Page 3: Operator Overloading

Operator Overloading

• There are two operators that will work with user defined objects.– The assignment operator (=)

• The assignment operator will do a member wise assignment of the data members of the class.

– The address operator (&)• The address operator simply returns the

address of the object in memory.

Page 4: Operator Overloading

Operator Overloading

• Page 526 lists all the operators that may be overloaded as well as the operators that can not be overloaded.– There are 5 operators that can not be

overloaded.. .* :: ?: sizeof

Page 5: Operator Overloading

Operator Overloading

• Overloading an operator does not change:– the operator precedence,– the associativity of the operator, – the arity of the operator, or– the meaning of how the operator works on

objects of built-in types.

Page 6: Operator Overloading

Operator Overloading

• Each individual operator must be overloaded for use with user defined types.– Overloading the assignment operator and

the subtraction operator does not overload the -= operator.

Page 7: Operator Overloading

Operator Functions

• Operator functions may be defined as either member functions or as non-member functions.– Non-member functions are usually made

friends for performance reasons.– Member functions usually use the this

pointer implicitly.

Page 8: Operator Overloading

Operator Functions

• The operator overloading functions for overloading (), [], -> or the assignment operators must be declared as a class member.

• All other operators may be declared as non-member functions.

Page 9: Operator Overloading

Member Functions

• When an operator function is implemented as a member function:– The leftmost operator must be a class

object or reference to a class object of the operator’s class.

– If the function must access private or protected data, then the function must be defined as a friend function.

Page 10: Operator Overloading

Non-member Functions

• When an operator function is implemented as a non-member function:– The left-most operand may be an object of

the operator’s class, an object of a different class, or a built-in type.

Page 11: Operator Overloading

Non-member Functions

• Non-member functions are not required to be defined as friends if the class contains the appropriate set and get methods are defined as public.

Page 12: Operator Overloading

Complex Numbers• Complex numbers consist of two pairs the

real and the imaginary parts.realPart + imaginaryPart * i where i has a value

– A program should be able to input and output complex numbers.

– A program should be able to add, subtract, and multiply complex numbers.

– A program should also be able to compare complex numbers.

Page 13: Operator Overloading

Overloading Unary Operators

• Unary operators can be overloaded as:– non-static member functions with no

arguments,or as– non-member functions with one argument

where the argument must be either an object of the class or a reference to an object of the class.

Page 14: Operator Overloading

Overloading Unary Operators

• The preference for overloading unary operators is to make the operator functions class members instead of non-member friend functions.

Page 15: Operator Overloading

Overloading Binary Operators

• Binary operators can be overloaded as:– non-static member functions with one

argument, or as– a non-member function with two arguments

where one of the arguments must be either a class object or a reference to a class object.

Page 16: Operator Overloading

Type Conversions

• Compilers do not know how to convert between user defined types and built-in types.– Such conversions must be defined by the

programmer using conversion constructors

Page 17: Operator Overloading

Type Conversions

• A conversion constructor (cast operator) is a single argument constructor that converts the objects of other types or built-in types into objects of a particular class.

• Overloading the cast operator must be a non-static member function, they can not be defined as friend functions.

Page 18: Operator Overloading

Type Conversion

• Overloaded casting operators do not have a return type.

Page 19: Operator Overloading

Overloading ++ and --

• When ++ and/or -- are overloaded, both pre-increment/decrement and post-increment/decrement must be overloaded.– Each version must have a distinct

signature.

Page 20: Operator Overloading

Overloading ++ and --

• It is standard practice to implement the pre-increment/decrement member function without out parameters.– Date &operator++();

• It is C++ standard to implement the post-increment/decrement member function with a dummy parameter of type int.– Date operator++(int );

– d1.operator++(0); // where the zero is used to indicate the signature for the post increment.

Page 21: Operator Overloading

Overloading ++ and --

• Date implementation for ++ and --.