300 likes | 308 Views
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++.
E N D
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.
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.
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.
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.
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
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
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!=().
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
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: • 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
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.
Examples • class IntegerMatrix { : : IntegerMatrix operator+( const Matrix &rightOp ); IntegerMatrix operator+( int scalar ); : :};
Examples IntegerMatrix m1(..), m2(..); int i(..); : : m1 = m1 + m2; m2 = m2 + i;
Examples • class StringArray { : : String &operator[]( int index ); : :}; • Define the indexing operator to work on StringArrays indexed with integers.
Examples • String &String::operator[]( int index ) .. • To use: StringArray stones(…); cout << “5th element = “ << stones[4]; int i( 0 ); stones[ i ] = “air“;
Examples • class String { : : String operator()( int start, int end ) const; : :}; • Define the function call operator to create a substring of the given string.
Examples • String String::operator()( int start, int end ) const .. • To use: String junk( “grandiose“ ); cout << junk( 2, 4 ) << endl; • Prints “and“
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.
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.
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 ); }
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.
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.
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.
Type Conversion • Overloaded casting operators do not have a return type. • The name of the operator implies its type.
Example class istream {:// return true iff stream// is OKoperator bool();: } while ( cin ) { … }
Example Class string {:string( const char *cStr );: }; String greeting( “hello“ );
Uh-oh… class A {A( const B &obj ); }; class B {operator A(); };
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.
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
Example • Date class