30
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 Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

Embed Size (px)

Citation preview

Page 1: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

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.

Page 2: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

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.

Page 3: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

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.

Page 4: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

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.

Page 5: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

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

Page 6: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

Operator Overloading

• Overloading an operator cannot change:– the operator precedence– the operator associativity– the arity of the operator– the definition of the operator on built-in

types

Page 7: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

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!=().

Page 8: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

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

Page 9: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

Overloading Unary Operators

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

Page 10: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

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

Page 11: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

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.

Page 12: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

Examples

• class IntegerMatrix {::IntegerMatrix operator+(

const Matrix &rightOp );

IntegerMatrix operator+( int scalar );

::

};

Page 13: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

Examples

IntegerMatrix m1(..), m2(..);

int i(..);

:

:

m1 = m1 + m2;

m2 = m2 + i;

Page 14: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

Examples

• class StringArray {::String &operator[]( int index );::

};

– Define the indexing operator to work on StringArrays indexed with integers.

Page 15: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

Examples

• String &String::operator[]( int index ) ..

– To use:StringArray stones(…);

cout << “5th element = “ << stones[4];

int i( 0 );

stones[ i ] = “air“;

Page 16: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

Examples

• class String {::String operator()(

int start, int end ) const;::

};

– Define the function call operator to create a substring of the given string.

Page 17: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

Examples

• String String::operator()(int start, int end ) const ..

– To use:String junk( “grandiose“ );

cout << junk( 2, 4 ) << endl;

– Prints “and“

Page 18: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

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.

Page 19: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

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.

Page 20: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

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 );

}

Page 21: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

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.

Page 22: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

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.

Page 23: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

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.

Page 24: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

Type Conversion

• Overloaded casting operators do not have a return type.

• The name of the operator implies its type.

Page 25: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

Example

class istream {:// return true iff stream// is OKoperator bool();:

}

while ( cin ) { … }

Page 26: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

Example

Class string {:string( const char *cStr );:

};

String greeting( “hello“ );

Page 27: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

Uh-oh…

class A {A( const B &obj );

};

class B {operator A();

};

Page 28: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

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.

Page 29: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

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

Page 30: Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations

Example

• Date class