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

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
Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
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.
 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.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 18 - Operator Overloading Outline 18.1Introduction 18.2Fundamentals of Operator Overloading 18.3Restrictions.
 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.
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.
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 Operator Overloading in C++ Copyright Kip Irvine, All rights reserved. Only students enrolled in COP 4338 at Florida International University may.
Chapter 12: Adding Functionality to Your Classes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
1 Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
 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.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
Operator Overloading in C++. Operator Overloading It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
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,
OPERATOR OVERLOADING Customised behaviour of operators.
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.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
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++)
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
Overloading Operator MySting Example
Object-Oriented Design (OOD) and C++
Developed By : Ms. K. S. Kotecha
Jim Fawcett Copyright ©
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
Overview of C++ Overloading
Operator Overloading.
CISC/CMPE320 - Prof. McLeod
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++
Operator Overloading I
Operator Overloading; String and Array Objects
Chapter 18 - Operator Overloading
Operator Overloading; String and Array Objects
Jim Fawcett Copyright ©
Presentation transcript:

Operator Overloading

Introduction Operator overloading Enabling C++’s operators to work with class objects Using traditional operators with user-defined objects Requires great care; when overloading is misused, program difficult to understand Examples of already overloaded operators Operator << is both the stream-insertion operator and the bitwise left-shift operator + and -, perform arithmetic on multiple types Compiler generates the appropriate code based on the manner in which the operator is used

Introduction Overloading an operator Using operators Write function definition as normal Function name is keyword operator followed by the symbol for the operator being overloaded operator+ used to overload the addition operator (+) Using operators To use an operator on a class object it must be overloaded unless the assignment operator(=)or the address operator(&) Assignment operator by default performs memberwise assignment Address operator (&) by default returns the address of an object

Restrictions on Operator Overloading C++ operators that can be overloaded C++ Operators that cannot be overloaded

Restrictions on Operator Overloading Overloading restrictions Precedence of an operator cannot be changed Associativity of an operator cannot be changed Arity (number of operands) cannot be changed Unary operators remain unary, and binary operators remain binary Operators &, *, + and - each have unary and binary versions Unary and binary versions can be overloaded separately No new operators can be created Use only existing operators No overloading operators for built-in types Cannot change how two integers are added Produces a syntax error

Operator Functions as Class Members vs. as friend Functions Member vs non-member Operator functions can be member or non-member functions When overloading ( ), [ ], -> or any of the assignment operators, must use a member function Operator functions as member functions Leftmost operand must be an object (or reference to an object) of the class If left operand of a different type, operator function must be a non-member function Operator functions as non-member functions Must be friends if needs to access private or protected members Enable the operator to be commutative

Overloading Stream-Insertion and Stream-Extraction Operators Overloaded << and >> operators Overloaded to perform input/output for user-defined types Left operand of types ostream & and istream & Must be a non-member function because left operand is not an object of the class Must be a friend function to access private data members

Overloading Unary Operators Can be overloaded with no arguments or one argument Should usually be implemented as member functions Avoid friend functions and classes because they violate the encapsulation of a class Example declaration as a member function: class String { public: bool operator!() const; ... };

Overloading Unary Operators Example declaration as a non-member function class String { friend bool operator!( const String & ) ... }

Overloading Binary Operators Overloaded Binary operators Non-static member function, one argument Example: class String { public: const String &operator+=( const String & ); ... }; y += z is equivalent to y.operator+=( z )

Overloading Binary Operators Non-member function, two arguments Example: class String { friend const String &operator+=( String &, const String & ); ... }; y += z is equivalent to operator+=( y, z )

Converting between Types Cast operator Forces conversions among built-in types Specifies conversions between user defined and built-in types Conversion operator must be a non-static member function Cannot be a friend function Do not specify return type Return type is the type to which the object is being converted For user-defined class A A::operator char *() const; Declares an overloaded cast operator function for creating a char * out of an A object