Operator Overloading in C++

Slides:



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

Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
 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
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
計算機程式 第十一單元 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.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 18 - C++ Operator Overloading Outline 18.1Introduction.
Chapter 14: Overloading and Templates
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 Operator Overloading; String and Array Objects.
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,
Operator overloading Object Oriented Programming.
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.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
Chapter 12: Adding Functionality to Your Classes.
 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.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
 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.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Operator Overloading.
Chapter 18 - C++ Operator Overloading
CSE1002 – Problem Solving with Object Oriented Programming
Chapter 13: Overloading and Templates
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; 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:

Operator Overloading in C++ Systems Programming

Operator Overloading Fundamentals of Operator Overloading Restrictions on Operator Overloading Operator Functions as Class Members vs. Global Functions Overloading Stream Insertion and Stream Extraction Operators Systems Programming Operator Overloading

Operator Overloading Overloading Unary Operators Overloading Binary Operators Case Study: Array Class Systems Programming Operator Overloading

Introduction Users can use operators with user-defined types (e.g.,with objects {operator overloading}). Clearer than function calls for certain classes. C++ makes operators sensitive to context. Examples: << Stream insertion, bitwise left-shift + Performs arithmetic on multiple items (integers, floats, pointers) Systems Programming Operator Overloading

Operator Overloading An operator is overloaded by writing: a non-static member function definition or a global function definition (non-member function definition in 7th edition of text) where the function name becomes the keyword operator followed by the symbol for the operation being overloaded. Systems Programming Operator Overloading

Operator Overloading Cannot create new operators! 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 the symbol Example function name operator+ to overload the addition operator + Systems Programming Operator Overloading

Operator Overloading To use an operator on a class object: The operator must be overloaded for that class. Three Exceptions: {overloading not required} Assignment operator (=) Performs “memberwise” assignment between objects Dangerous for classes with pointer members!! Address operator (&) Returns a pointer to the object. Comma operator (,) Evaluates the 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; Systems Programming Operator Overloading

Restrictions on Operator Overloading Overloading cannot change: The precedence of the 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. Systems Programming Operator Overloading

Fig. 22.1 Operators that can be overloaded. Systems Programming Operator Overloading

Fig. 19.2 Operators that cannot be overloaded. Copyright © Pearson, Inc. 2013. All Rights Reserved. Systems Programming Operator Overloading

Software Engineering Observation 22.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. Systems Programming Operator Overloading

22.4 Operator Functions as Class Members vs. Global Members Operator functions as member functions: Leftmost object must be of the same class as operator function. Use this keyword to implicitly get left operand argument. Operators (), [], -> or any of the assignment operators 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. Systems Programming Operator Overloading

22.4 Operator Functions as Class Members vs. Global Members Operator functions as global functions: Need parameters for both operands. Can have an object of a different class than the operator. Can be made a friend to access private or protected data. Systems Programming Operator Overloading

Overloading Stream Insertion and Stream Extraction Operators Overloaded << operator used where Left operand of type ostream & Such as cout object in cout << classObject To use the operator in this manner where the right operand is an object of a user-defined class, it must be overloaded as a global function. Similarly, overloaded >> has left operand of istream & Thus, both must be global functions. Systems Programming Operator Overloading

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 a member function. For the other way, you need a global overloaded function. long int + HugeIntClass Systems Programming Operator Overloading

19.5 Overloading Stream Insertion and Stream Extraction Operators << and >> operators Already overloaded to process each built-in type (pointers and strings). Can also process a user-defined class. Overload using global, friend functions Example program Class PhoneNumber Holds a telephone number Prints out formatted number automatically. (123) 456-7890 Systems Programming Operator Overloading

Overload Stream Insertion and Extraction Operators Notice function prototypes for overloaded operators >> and << (must be global, friend functions) Systems Programming Operator Overloading

Overload Stream Insertion and Extraction Operators Allows cout << phone; to be interpreted as: operator<< (cout, phone); Display formatted phone number Systems Programming Operator Overloading

Overload Stream Insertion and Extraction Operators ignore skips specified number of characters from input (1 by default) Input each portion of phone number separately Systems Programming Operator Overloading

Overload Stream Insertion and Extraction Operators Testing overloaded >> and << operators to input and output a PhoneNumber object Systems Programming Operator Overloading

Overload Stream Insertion and Extraction Operators Systems Programming Operator Overloading

19.6 Overloading Unary Operators Overloading unary operators of a class: Can overload as a non-static member function with no arguments. OR Can overload as a global function with one argument. Argument must be class object or reference to class object. Remember, static functions only access static data. Systems Programming Operator Overloading

22.6 Overloading Unary Operators Example Overload ! to test for an empty string Consider the expression !s in which s is an object of class String. For !s the compiler generates the call s.operator!( ) Namely, since it is a non-static member function, it needs no arguments: class String { public: bool operator!() const; … }; // end class String If a global function, it needs one argument: bool operator!( const String & ) !s becomes operator!(s) Systems Programming Operator Overloading

22.7 Overloading Binary Operators Non-static member function with one argument. or Global function with two arguments: One argument must be class object or reference to a class object. Systems Programming Operator Overloading

22.7 Overloading Binary Operators If a non-static member function, it needs one argument. class String { public: const String & operator+=( const String & ); … }; y += z becomes y.operator+=( z ) If a global function, it needs two arguments. const String & operator+=( String &, const String & ); y += z becomes operator+=( y, z ) Clear example of conciseness of operator overload Systems Programming Operator Overloading

Overloading Operators On the previous slide, y and z are assumed to be String-class objects or references to String-class objects. There are two ways to pass arguments to the global function, either with an argument that is an object (this requires a copy of the object) or with an argument that is a reference to an object (this means the side effects of the function called to implement the overloaded operator can side-effect this object that is called-by-reference!) Systems Programming Operator Overloading

19.10 Case Study: Array Class Problems with 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. {Basic point of this chapter – by using C++ classes and operator overloading, one can significantly change the capabilities of the built in array type.} Systems Programming Operator Overloading

Case Study: Array Class Case Study: Implement an Array class with: Range checking Array assignment ( = ) Arrays that know their own size. Outputting/inputting entire arrays with << and >> Array comparisons with == and != Systems Programming Operator Overloading

Case Study: Array Class Copy constructor Used whenever copy of object is needed: Passing by value (return value or parameter). Initializing an object with a copy of another object of the same type. Array newArray( oldArray ); or Array newArray = oldArray; (both are identical) newArray is a copy of oldArray. Systems Programming Operator Overloading

Case Study: Array Class Prototype for class Array Array( const Array & ); //copy constructor Must take a reference Otherwise, the argument will be passed by value… Which tries to make a copy by calling copy constructor… This yields an infinite loop! Systems Programming Operator Overloading

Case Study: Array Class Prototype for copy constructor Most operators overloaded as member functions (except << and >> which must be global functions) Prototype for copy constructor != operator simply returns opposite of == operator – only need to define the == operator Systems Programming Operator Overloading

Case Study: Array Class Operators for accessing specific elements of Array object Note: An example of pointer data member Systems Programming Operator Overloading

Case Study: Array Class Note: Standard method for using pointer to access an array of objects. Systems Programming Operator Overloading

Case Study: Array Class We must declare a new integer array so the objects do not point to the same memory Systems Programming Operator Overloading

Case Study: Array Class Want to avoid self assignment This would be dangerous if this is the same Array as right Systems Programming Operator Overloading

Case Study: Array Class integers1[ 5 ] calls integers1.operator[]( 5 ) Systems Programming Operator Overloading

Case Study: Array Class Systems Programming Operator Overloading

Case Study: Array Class Systems Programming Operator Overloading

Case Study: Array Class Retrieve number of elements in Array Use overloaded >> operator to input Systems Programming Operator Overloading

Case Study: Array Class Use overloaded << operator to output Use overloaded != operator to test for inequality Use copy constructor Use overloaded = operator to assign Systems Programming Operator Overloading

Case Study: Array Class Use overloaded == operator to test for equality Use overloaded [] operator to access individual integers, with range-checking Systems Programming Operator Overloading

Case Study: Array Class Systems Programming Operator Overloading

Case Study: Array Class Systems Programming Operator Overloading

Review of Operator Overloading Covered operator overloading basics. Reviewed operator overloading restrictions. Explained when to use class member functions and when to use global functions to implement operator overloading. Discussed overloading stream insertion and stream extraction operators and did one simple example of overloading. Systems Programming Operator Overloading

Review of Operator Overloading Went through overloading unary and binary operators. Looked at operator overloading in an elaborate case study involving an Array class. Several good C++ concepts in this example including the copy constructor!! Systems Programming Operator Overloading