1 / 30

Operator Overloading

Operator overloading allows programmers to define new uses of existing C/C++ operator symbols, useful for defining common operations for user-defined types. Learn how to implement operator overloading in C/C++.

wpool
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 uses of the existing C/C++ operator symbols. • useful for defining common operations for user defined types. • should be used to perform the same or similar function on class objects as the function built in for the basic types.

  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 uses the binary division operator. • operator!() will define a function that uses the unary negation operator.

  3. Operator Functions • Operator functions may be defined as either member functions or as non-member functions. • Non-member functions are frequently made friends for performance reasons. • A member operator function uses the target object as its first operand.

  4. Operator Functions • The operator overloading functions for overloading (), [], -> or the assignment operators must be declared as class member functions. • All other operators may be declared as non-member functions.

  5. Operator Overloading • The handout lists all the operators that may be overloaded as well as the operators that can not be overloaded. • There are 5 operators that cannot be overloaded. . .* :: ?: sizeof

  6. Operator Overloading • Overloading an operator cannotchange: • the operator precedence • the operator associativity • the arity of the operator • the definition of the operator on built-in types

  7. Operator Overloading • Each individual operator must be defined separately. • For example: • overloading operator=() and operator-() does not overload operator-=(). • overloading operator==() does not overload operator!=().

  8. Overloading Unary Operators • Unary operators can be overloaded as: • member functions with no arguments • static, friend, or global functions with one argument where the argument must be either a class or a reference to a class

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

  10. Overloading Binary Operators • Binary operators can be overloaded as: • member functions with one argument • static, friend, or global functions with two arguments where at least one of the arguments must be either a class or a reference to a class

  11. Member Functions • When an operator function is implemented as a member function: • The leftmost operand becomes an instance of the class of which the function is a member.

  12. Examples • class IntegerMatrix { : : IntegerMatrix operator+( const Matrix &rightOp ); IntegerMatrix operator+( int scalar ); : :};

  13. Examples IntegerMatrix m1(..), m2(..); int i(..); : : m1 = m1 + m2; m2 = m2 + i;

  14. Examples • class StringArray { : : String &operator[]( int index ); : :}; • Define the indexing operator to work on StringArrays indexed with integers.

  15. Examples • String &String::operator[]( int index ) .. • To use: StringArray stones(…); cout << “5th element = “ << stones[4]; int i( 0 ); stones[ i ] = “air“;

  16. Examples • class String { : : String operator()( int start, int end ) const; : :}; • Define the function call operator to create a substring of the given string.

  17. Examples • String String::operator()( int start, int end ) const .. • To use: String junk( “grandiose“ ); cout << junk( 2, 4 ) << endl; • Prints “and“

  18. Operator Overloading • There are two operators that have default definitions for user defined objects. • The assignment operator (=) • The assignment operator will do a member wise assignment of the data of the class. • The address operator (&) • The address operator simply returns the address of the object in memory.

  19. Non-member Functions • When an operator function is implemented as a non-member function: • The left-most operand may be any type, as long as at least one operand is a class. • If the function must access non-public data, then the function must either • be defined as a friend function, or • use public accessor and mutator functions.

  20. Example MyType *operator&( MyType &m ) {std::cerr << “You have violated the C++ style guidelines and will be asked to leave the future immediately.“ << std::endl;exit( 86 ); }

  21. Complex Numbers • Complex numbers consist of two real numbers: the real and the imaginary parts. realPart + imaginaryPart * i where i = sqrt(-1) • 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.

  22. 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 and conversion operators.

  23. Type Conversions • A conversion constructor (type 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 a cast operator must be a non-static member function; it cannot be defined as an external or friend function.

  24. Type Conversion • Overloaded casting operators do not have a return type. • The name of the operator implies its type.

  25. Example class istream {:// return true iff stream// is OKoperator bool();: } while ( cin ) { … }

  26. Example Class string {:string( const char *cStr );: }; String greeting( “hello“ );

  27. Uh-oh… class A {A( const B &obj ); }; class B {operator A(); };

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

  29. Overloading ++ and -- • The pre-increment/decrement member function is declared without any parameters. • The post-increment/decrement member function is declared with a dummy parameter of type int. • Date &operator++(); • Date &operator++( int ); • Date d; • ++d; // calls the first one • d++; // calls the second one

  30. Example • Date class

More Related