Operator Overloading

Post on 07-Jan-2016

23 views 0 download

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

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.

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.

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.

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

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.

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.

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.

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.

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.

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.

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.

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.

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.

Overloading Unary Operators

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

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.

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

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.

Type Conversion

• Overloaded casting operators do not have a return type.

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.

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.

Overloading ++ and --

• Date implementation for ++ and --.