Operator Overloading in C Updated 11112003 Copyright Kip



![Commonly Overloaded Operators l Unary ++, --, ( ), [ ], +, - l Commonly Overloaded Operators l Unary ++, --, ( ), [ ], +, - l](https://slidetodoc.com/presentation_image/49340074a0d96eb1c533cb62ded24a26/image-4.jpg)




























![Overloading the [ ] Operator Returns a modifiable reference to a student. Permits the Overloading the [ ] Operator Returns a modifiable reference to a student. Permits the](https://slidetodoc.com/presentation_image/49340074a0d96eb1c533cb62ded24a26/image-33.jpg)
![Calling the [ ] Operator Use this operator to insert a student in the Calling the [ ] Operator Use this operator to insert a student in the](https://slidetodoc.com/presentation_image/49340074a0d96eb1c533cb62ded24a26/image-34.jpg)
![Overloading the [ ] Operator (cont) This version returns a constant reference. const Student Overloading the [ ] Operator (cont) This version returns a constant reference. const Student](https://slidetodoc.com/presentation_image/49340074a0d96eb1c533cb62ded24a26/image-35.jpg)
![Calling the const & [ ] Operator This version of the operator is called Calling the const & [ ] Operator This version of the operator is called](https://slidetodoc.com/presentation_image/49340074a0d96eb1c533cb62ded24a26/image-36.jpg)

- Slides: 37
Operator Overloading in C++ Updated 11/11/2003 Copyright Kip Irvine, 2003. All rights reserved. Only students enrolled in COP 4338 at Florida International University may copy or print the contents of this slide show. Some materials used here are from Mark Allen Weiss, used by permission. 1
Overview l Operator Overloading Basics • Commonly overloaded operators • Requirements l Time Class • Exception handling example • Overloading ++, +=, < • Implicit conversion operators • Stream output ( << ) l Using a Class to Encapsulate a vector • Student. Vector class • overloading [ ] • Stream output ( << ) Copyright Kip Irvine, 2003 2
Operator Overloading Basics l Permit standard operators to be used with your class objects • keep the traditional semantics l Operator always associated with a class • may or may not be a member function l l Unary and binary operators Syntax: • ret. Type operator op ( paramlist ) • op is some standard operator symbol Copyright Kip Irvine, 2003 3
Commonly Overloaded Operators l Unary ++, --, ( ), [ ], +, - l (increment, decrement) (function call) (subscript) (sign) Binary +, -, *, /, % =, +=, -+, *=, /=, %= &, |, ^, ^=, &=, |= ==, !=, >, <, >=, <= ||, && <<, >> (arithmetic) (assignment) (bitwise) (relational) (logical) (shift) Copyright Kip Irvine, 2003 4
Requirements l l Must be nonstatic member, or global function having at least 1 class parameter Cannot overload operators dealing with only primitives • example: int operator + ( int x, int y ); Copyright Kip Irvine, 2003 5
Requirements l (cont) Cannot change operator precedence • binary * will be higher than binary + l Cannot change the number of operands • for example, operator / cannot be unary • operator ~ cannot be binary l Cannot change associativity 3 • Example: 62 implies pow(6, pow(2, 3)) l should not be written as 6^2^3, because ^ associates left to right Copyright Kip Irvine, 2003 6
Time Class class Time { public: Time( unsigned int c = 0 ); // Time( const Time & t ); // operator int( ) const // operator string( ) const // const Time & operator ++( ); // Time operator ++( int ); // bool operator <( const Time & rhs ) const // const Time & operator +=( unsigned n ); // friend ostream & operator <<( ostream & os, const Time & h ); private: unsigned hours; unsigned minutes; }; Copyright Kip Irvine, 2003 constructor convert to int convert to string prefix increment postfix increment less-than add-assign // stream output 7
Overloading ++ l l Prefix and postfix increment operators Time class: increment the minutes. const Time & operator ++( ); Time operator ++( int ); Time T( 1245 ); T++; ++T; // prefix // postfix // 12: 45 pm // 12: 46 pm // 12: 47 pm Copyright Kip Irvine, 2003 8
Prefix ++ // Prefix increment: add 1 to minutes. If // end of hour reached, reset to beginning // of next hour. const Time & operator ++( ) { if( ++minutes > 59 ) { minutes = 0; hours = ( hours + 1) % ( 24 ); } return *this; } Copyright Kip Irvine, 2003 9
Postfix ++ // postfix increment: return the current // time, then increment the minutes. Time operator ++( int ) { Time save( *this ); operator ++( ); return save; } // construct a copy // increment the time // return the copy Copyright Kip Irvine, 2003 10
Addition-Assignmment ( += ) // Add n minutes to the time. const Time & operator +=( unsigned n ) { unsigned t = minutes + n; minutes = t % 60; // remaining minutes hours += (t / 60); // add to hours = hours % 24; // roll to next day return *this; } Copyright Kip Irvine, 2003 11
Less-Than Operator ( < ) bool operator <( const Time & rhs ) const { if( hours < rhs. hours ) return true; else if( hours == rhs. hours ) return minutes < rhs. minutes; else return false; } called as: if( t 1 < t 2 ). . . Copyright Kip Irvine, 2003 12
Implcit Convert-to-int class Time { operator int( ) const { return (hours * 100) + minutes; } //. . . }; called as: Time t 1( 1830 ); int n = t 1; Copyright Kip Irvine, 2003 13
Implcit Convert-to-string class Time { operator string( ) const { ostrstream os; os << hours << ": " << minutes << '