 2006 Pearson Education, Inc. All rights reserved. 1 11 Operator Overloading.

Slides:



Advertisements
Similar presentations
Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - Operator Overloading Outline 8.1 Introduction 8.2 Fundamentals of Operator Overloading 8.3.
Operator Overloading Fundamentals
Classes: A Deeper Look Systems Programming.  constconst  const objects and const member functions   Composition   Friendship  this  this pointer.
Class and Objects.
計算機程式 第十一單元 Operator Overloading I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to.
Operator Overloading. Basics Define operations on user-defined data types –Perform same operation as it is supposed to but, with operands of the user-defined.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 18 - C++ Operator Overloading Outline 18.1Introduction.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 18 - Operator Overloading Outline 18.1Introduction 18.2Fundamentals of Operator Overloading 18.3Restrictions.
More on Operator Overloading CS-2303, C-Term More on Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Classes: A Deeper Look Systems Programming.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Chapter 13: Overloading.
Chapter 15: Operator Overloading
 2008 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator overloading Object Oriented Programming.
Operator Overloading in C++
1 CSC241: Object Oriented Programming Lecture No 07.
Review of C++ Programming Part II Sheng-Fang Huang.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
CS Object Oriented Programming Using C++
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 2009.
1 Chapter 8 - Operator Overloading Outline 8.1Introduction 8.2Fundamentals of Operator Overloading 8.3Restrictions on Operator Overloading 8.4Operator.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
1 CSC241: Object Oriented Programming Lecture No 08.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
 2008 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
CSE1002 – Problem Solving with Object Oriented Programming
Chapter 10 Operator Overloading; Class string
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Operator Overloading; String and Array Objects
Operator Overloading; Class string
Chapter 14: More About Classes.
Operator Overloading; String and Array Objects
Operator Overloading.
Operator Overloading; String and Array Objects
Operator Overloading.
Operator Overloading; String and Array Objects
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
9-10 Classes: A Deeper Look.
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Operator Overloading; String and Array Objects
Chapter 18 - Operator Overloading
Operator Overloading; String and Array Objects
9-10 Classes: A Deeper Look.
Presentation transcript:

 2006 Pearson Education, Inc. All rights reserved Operator Overloading

 2006 Pearson Education, Inc. All rights reserved Introduction Use operators with objects (operator overloading) – Clearer than function calls for certain classes – Operator sensitive to context Examples – << Stream insertion, bitwise left-shift – + Performs arithmetic on multiple items (integers, floats, etc.)

 2006 Pearson Education, Inc. All rights reserved Fundamentals of Operator Overloading Types for operator overloading – Built in ( int, char ) or user-defined (classes) – Can use existing operators with user-defined types Cannot create new operators Overloading operators – Create a function for the class – Name of operator function Keyword operator followed by symbol – Example operator+ for the addition operator +

 2006 Pearson Education, Inc. All rights reserved Fundamentals of Operator Overloading (Cont.) Using operators on a class object – It must be overloaded for that class Exceptions: (can also be overloaded by the programmer) – Assignment operator ( = ) Memberwise assignment between objects – Address operator ( & ) Returns address of object – Comma operator (, ) Evaluates expression to its left then the expression to its right Returns the value of the expression to its right Overloading provides concise notation – object2 = object1.add( object2 ); vs. object2 = object2 + object1;

 2006 Pearson Education, Inc. All rights reserved Restrictions on Operator Overloading Cannot change – Precedence of operator (order of evaluation) Use parentheses to force order of operators – Associativity (left-to-right or right-to-left) – Number of operands e.g., & is unary, can only act on one operand – How operators act on built-in data types (i.e., cannot change integer addition) Cannot create new operators Operators must be overloaded explicitly – Overloading + and = does not overload += Operator ?: cannot be overloaded

 2006 Pearson Education, Inc. All rights reserved. 6 Fig | Operators that cannot be overloaded. Fig | Operators that can be overloaded.

 2006 Pearson Education, Inc. All rights reserved. 7 Software Engineering Observation 11.2 At least one argument of an operator function must be an object or reference of a user-defined type. This prevents programmers from changing how operators work on fundamental types.

 2006 Pearson Education, Inc. All rights reserved. 8 Common Programming Error 11.5 Assuming that overloading an operator such as + overloads related operators such as += or that overloading == overloads a related operator like != can lead to errors. Operators can be overloaded only explicitly; there is no implicit overloading.

 2006 Pearson Education, Inc. All rights reserved Operator Functions as Class Members vs. Global Members Operator functions – As member functions Leftmost object must be of same class as operator function Use this keyword to implicitly get left operand argument Operators (), [], -> or any assignment operator must be overloaded as a class member function Called when – Left operand of binary operator is of this class – Single operand of unary operator is of this class – As global functions Need parameters for both operands Can have object of different class than operator Can be a friend to access private or protected data

 2006 Pearson Education, Inc. All rights reserved Operator Functions as Class Members vs. Global Members (Cont.) Overloaded << operator – Left operand of type ostream & Such as cout object in cout << classObject – Similarly, overloaded >> has left operand of istream & – Thus, both must be global functions > operators – Already overloaded to process each built-in type – Can also process a user-defined class Overload using global, friend functions

 2006 Pearson Education, Inc. All rights reserved Operator Functions as Class Members vs. Global Members (Cont.) Commutative operators – May want + to be commutative So both “ a + b ” and “ b + a ” work – Suppose we have two different classes Overloaded operator can only be member function when its class is on left – HugeIntClass + long int Can be member function When other way, need a global overloaded function – long int + HugeIntClass

 2006 Pearson Education, Inc. All rights reserved Overloading Unary Operators Overloading unary operators – Can overload as non- static member function with no arguments – Can overload as global function with one argument Argument must be class object or reference to class object – Remember, static functions only access static data

 2006 Pearson Education, Inc. All rights reserved Overloading Unary Operators (Cont.) Example: – Overload ! to test for empty string – If non- static member function, needs no arguments class String { public: bool operator!() const; … }; !s becomes s.operator!() – If global function, needs one argument bool operator!( const String & ) !s becomes operator!(s)

 2006 Pearson Education, Inc. All rights reserved Overloading Binary Operators Overloading binary operators – Non- static member function, one argument – Global function, two arguments One argument must be class object or reference

 2006 Pearson Education, Inc. All rights reserved Overloading Binary Operators (Cont.) Example: Overloading += – If non- static member function, needs one argument class String { public: const String & operator+=( const String & ); … }; y += z becomes y.operator+=( z ) – If global function, needs two arguments const String & operator+=(String &, const String &); y += z becomes operator+=( y, z )

 2006 Pearson Education, Inc. All rights reserved Case Study: Array Class Pointer-based arrays in C++ – No range checking – Cannot be compared meaningfully with == – No array assignment (array names are const pointers) – If array passed to a function, size must be passed as a separate argument Example: Implement an Array class with – Range checking – Array assignment – Arrays that know their own size – Outputting/inputting entire arrays with > – Array comparisons with == and !=

 2006 Pearson Education, Inc. All rights reserved Case Study: Array Class (Cont.) Copy constructor – Used whenever copy of object is needed: Passing by value (return value or parameter) Initializing an object with a copy of another of same type – Array newArray( oldArray ); or Array newArray = oldArray (both are identical) newArray is a copy of oldArray – Prototype for class Array Array( const Array & ); Must take reference – Otherwise, the argument will be passed by value… – Which tries to make copy by calling copy constructor… Infinite loop

 2006 Pearson Education, Inc. All rights reserved. 18 Outline Array.h

 2006 Pearson Education, Inc. All rights reserved. 19 Outline Array.cpp (1 of 4)

 2006 Pearson Education, Inc. All rights reserved. 20 Outline Array.cpp (2 of 4)

 2006 Pearson Education, Inc. All rights reserved. 21 Outline Array.cpp (3 of 4)

 2006 Pearson Education, Inc. All rights reserved. 22 Outline Array.cpp (4 of 4)

 2006 Pearson Education, Inc. All rights reserved. 23 Outline fig11_08.cpp (1 of 4)

 2006 Pearson Education, Inc. All rights reserved. 24 Outline fig11_08.cpp (2 of 4)

 2006 Pearson Education, Inc. All rights reserved. 25 Outline fig11_08.cpp (3 of 4)

 2006 Pearson Education, Inc. All rights reserved. 26 Outline fig11_08.cpp (4 of 4)

 2006 Pearson Education, Inc. All rights reserved. 27 Software Engineering Observation 11.4 The argument to a copy constructor should be a const reference to allow a const object to be copied.

 2006 Pearson Education, Inc. All rights reserved. 28 Common Programming Error 11.6 Note that a copy constructor must receive its argument by reference, not by value. Otherwise, the copy constructor call results in infinite recursion (a fatal logic error) because receiving an object by value requires the copy constructor to make a copy of the argument object.

 2006 Pearson Education, Inc. All rights reserved. 29 Common Programming Error 11.7 If the copy constructor simply copied the pointer in the source object to the target object’s pointer, then both objects would point to the same dynamically allocated memory. The first destructor to execute would then delete the dynamically allocated memory, and the other object’s ptr would be undefined, a situation called a dangling pointer—this would likely result in a serious run-time error (such as early program termination) when the pointer was used.

 2006 Pearson Education, Inc. All rights reserved. 30 Error-Prevention Tip 11.1 Returning a reference from an overloaded > operator function is typically successful because cout, cin and most stream objects are global, or at least long-lived. Returning a reference to an automatic variable or other temporary object is dangerous—creating “dangling references” to nonexisting objects.

 2006 Pearson Education, Inc. All rights reserved Overloading ++ and -- Increment/decrement operators can be overloaded – Suppose we want to add 1 to a Date object, d1 – Prototype (member function) Date &operator++(); ++d1 becomes d1.operator++() – Prototype (global function) Date &operator++( Date & ); ++d1 becomes operator++( d1 )

 2006 Pearson Education, Inc. All rights reserved Overloading ++ and -- (Cont.) To distinguish prefix and postfix increment – Postfix increment has a dummy parameter An int with value 0 – Prototype (member function) Date operator++( int ); d1++ becomes d1.operator++( 0 ) – Prototype (global function) Date operator++( Date &, int ); d1++ becomes operator++( d1, 0 )

 2006 Pearson Education, Inc. All rights reserved Overloading ++ and -- (Cont.) Return values – Prefix increment Returns by reference ( Date & ) lvalue (can be assigned) – Postfix increment Returns by value – Returns temporary object with old value rvalue (cannot be on left side of assignment) All this applies to decrement operators as well

 2006 Pearson Education, Inc. All rights reserved Case Study: A Date Class Example Date class – Overloaded increment operator Change day, month and year – Overloaded += operator – Function to test for leap years – Function to determine if day is last of month

 2006 Pearson Education, Inc. All rights reserved. 35 Outline Date.h (1 of 1)

 2006 Pearson Education, Inc. All rights reserved. 36 Outline Date.cpp (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 37 Outline Date.cpp (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 38 Outline fig11_14.cpp (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 39 Outline fig11_14.cpp (2 of 2)