1 / 21

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.

landon
Download Presentation

Operator Overloading

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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.

  3. 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.

  4. 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

  5. Operator Overloading • Overloading an operator does notchange: • 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. 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.

  11. 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.

  12. 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.

  13. 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.

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

  15. 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.

  16. 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

  17. 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.

  18. Type Conversion • Overloaded casting operators do not have a return type.

  19. 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.

  20. 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.

  21. Overloading ++ and -- • Date implementation for ++ and --.

More Related