Operator OverloadingCS-2303, C-Term 20101 Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,

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
Class and Objects.
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.
A Deeper Look at Classes CS-2303, C-Term A Deeper Look at Classes CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Chapter 13: Overloading.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Chapter 15: Operator Overloading
 2008 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
CS-2303 System Programming Concepts
Operator overloading Object Oriented Programming.
Operator Overloading in C++
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
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.
Chapter 12: Adding Functionality to Your Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
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.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
CS Object Oriented Programming Using C++
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
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
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Operator Overloading; String and Array Objects
Polymorphism in C++ Operator Overloading
Chapter 15: Overloading and Templates
Operator Overloading BCA Sem III K.I.R.A.S.
Operator Overloading; String and Array Objects
Operator Overloading.
Operator Overloading; String and Array Objects
Miscellaneous C++ Topics
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,
COP 3330 Object-oriented Programming in C++
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Operator Overloading; String and Array Objects
Chapter 18 - Operator Overloading
A Deeper Look at Classes
Operator Overloading; String and Array Objects
Presentation transcript:

Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel, and from The C++ Programming Language, 3 rd edition, by Bjarne Stroustrup)

Operator OverloadingCS-2303, C-Term Why Operator Overloading? Readable code Extension of language to include user- defined types I.e., classes Make operators sensitive to context Generalization of function overloading

Operator OverloadingCS-2303, C-Term Simple Example class complex { double real, imag; public: complex(double r, double i) : real(r), imag(i) {} } Would like to be able to write:– complex a = complex(1, 3.0); complex b = complex(1.2, 2); complex c = b; a = b + c; b = b+c*a; c = a*b + complex(1,2); I.e., would like to write ordinary arithmetic expressions on this user-defined class.

Operator OverloadingCS-2303, C-Term With Operator Overloading, We Can class complex { double real, imag; public: complex(double r, double i) : real(r), imag(i) {} complex operator+(complex a, complex b); complex operator*(complex a, complex b); complex& operator=(complex a, complex b);... }

Operator OverloadingCS-2303, C-Term General Format returnType operator*(parameters);    any type keyword operator symbol Return type may be whatever the operator returns –Including a reference to the object of the operand Operator symbol may be any overloadable operator from the list.

Operator OverloadingCS-2303, C-Term C++ Philosophy All operators have context Even the simple “built-in” operators of basic types E.g., '+', '-', '*', '/' for numerical types Compiler generators different code depending upon type of operands Operator overloading is a generalization of this feature to non-built-in types E.g., ' >' for bit-shift operations and also for stream operations

Operator OverloadingCS-2303, C-Term C++ Philosophy (continued) Operators retain their precedence and associativity, even when overloaded Operators retain their number of operands Cannot redefine operators on built-in types Not possible to define new operators Only (a subset of) the built-in C++ operators can be overloaded

Operator OverloadingCS-2303, C-Term Operators that Can and Cannot be Overloaded Deitel & Deitel, Figure 22.1 Deitel & Deitel, Figure 22.2

Operator OverloadingCS-2303, C-Term Outline Fundamentals of Operator Overloading Restrictions on Operator Overloading Operator Functions as Class Members vs. Global Functions Overloading Stream Insertion and Stream Extraction Operators

Operator OverloadingCS-2303, C-Term Operator Overload Function Either static member function definition – a non-static member function definitionor –a global function definition –Usually a friend of the class Function “name” is keyword operator followed by the symbol for the operation being overloadedFunction “name” is keyword operator followed by the symbol for the operation being overloaded –E.g. operator+, operator=

Operator OverloadingCS-2303, C-Term Operator Overload Function (continued) Operator overload function is a function just like any other Can be called like any other – e.g., a.operator+(b) C++ provides the following short-hand a+b

Operator OverloadingCS-2303, C-Term Operator Overload Function (continued) If operator overload function is declared global then operator+(a, b) also reduces to the following short-hand a+b

Operator OverloadingCS-2303, C-Term Operator Overloading (continued) To use any operators on a class object, … –The operator must be overloaded for that class. Three Exceptions: {overloading not required} = –Assignment operator (=) Memberwise assignment between objects Dangerous for classes with pointer members!! & –Address operator (&) Returns address of the object in memory., –Comma operator (,) Evaluates expression to its left then the expression to its right. Returns the value of the expression to its right.

Operator OverloadingCS-2303, C-Term Questions?

Operator OverloadingCS-2303, C-Term Operator Functions as Class Members Leftmost operand 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.

Operator OverloadingCS-2303, C-Term Operator Functions as Global Members Need parameters for both operands. Can have object of different class than operator. Can be made a friend to access private or protected data.

Operator OverloadingCS-2303, C-Term Stream Insertion and Extraction Operators as Global Functions Overload << operator used where –Left operand of type ostream & Such as cout object in cout << classObject Overload >> has left operand of istream & –Left operand of type istream & Such as cin object in cout >> classObject Reason:– –These operators are associated with class of right operand

Operator OverloadingCS-2303, C-Term Commutative operators May need + to be commutative So both “ a + b ” and “ b + a ” work as expected. 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 –For the other way, you need a global overloaded function. –long int + HugeIntClass

Operator OverloadingCS-2303, C-Term Example – Deitel & Deitel §23.5 > 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)

Operator OverloadingCS-2303, C-Term Notice function prototypes for overloaded operators >> and << (must be global, friend functions) Example §23.5 (continued) Note also: returns a reference!

Operator OverloadingCS-2303, C-Term Display formatted phone number Allows cout << phone; to be interpreted as: operator<<(cout, phone); Example §23.5 (continued)

Operator OverloadingCS-2303, C-Term Input each portion of phone number separately ignore skips specified number of characters from input (1 by default) Example §23.5 (continued)

Operator OverloadingCS-2303, C-Term Invoke overloaded >> and << operators to input and output a PhoneNumber object Example §23.5 (concluded)

Operator OverloadingCS-2303, C-Term Questions?

Operator OverloadingCS-2303, C-Term Unary Operators Can overload as –Non- static member function with no arguments. –As a global function with one argument. Argument must be class object or reference to class object. Why non- static ? –static functions only access static data –Not what is needed for operator functions

Operator OverloadingCS-2303, C-Term Example – D & D §22.6 Overload '!' to test for empty string When compiler encounters while (!s) … if (!s) … it generates the call to s.operator!() Implemented as:– class String { public: bool operator!() const; … };

Operator OverloadingCS-2303, C-Term Overloading Binary Operators staticNon-static member function with one argument. or Global function with two arguments: –One argument must be class object or reference to a class object. This is the mechanism by which the compiler prevents you from redefining built-in operations!

Operator OverloadingCS-2303, C-Term Overloading Binary Operators (continued) If a non- static member function, it needs one argument. class String { public: String & operator+=( const String &); … }; By shorthand rule y += z becomes y.operator+=( z )

Operator OverloadingCS-2303, C-Term Overloading Binary Operators (continued) If a global function, it needs two arguments class String { public: String & operator+=( String &, const String & ); … }; By short-hand rule y += z becomes operator+=(y, z)

Operator OverloadingCS-2303, C-Term Overloading Operators y zOn 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:– –With an argument that is an object (this requires a copy of the object) or side-effect –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!)

Operator OverloadingCS-2303, C-Term Questions?

Operator OverloadingCS-2303, C-Term Next Time Array Class –Deitel & Deitel