Operator Overloading I

Slides:



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

Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
 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.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 18 - Operator Overloading Outline 18.1Introduction 18.2Fundamentals of Operator Overloading 18.3Restrictions.
OOP Egar 2008 – Recitation 41 Object Oriented Programming Spring 2006 Recitation 6.
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++
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Review of C++ Programming Part II Sheng-Fang Huang.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
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:
C++ Lecture 7 Function/operator overloading
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.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
Operator overloading and type convesions BCAS,Bapatla B.mohini devi.
Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Operator Overloading. Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
Object Oriented Programming COP3330 / CGS5409.  Arithmetic Operator Overloading  Increment (++) / Decrement (--)  In-class exercise.
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Operator Overloading.
Chapter 18 - C++ Operator Overloading
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading CS 3370 – C++ Chapter 14.
Operator Overloading Introduction
Overloading C++ supports the concept of overloading Two main types
Chapter 14: More About Classes.
Operator Overloading; String and Array Objects
Overloading Operator MySting Example
Visit for more Learning Resources
Object Oriented Programming COP3330 / CGS5409
CS 2304: Operator Overloading
Polymorphism in C++ Operator Overloading
Constructor & Destructor
Class: Special Topics Copy Constructors Static members Friends this
Jim Fawcett Copyright ©
More about OOP and ADTs Classes
Chapter 14: More About Classes.
Operators Lecture 10 Fri, Feb 8, 2008.
Operator Overloading; String and Array Objects
Operator Overloading.
Function Overloading C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define similar.
Operator Overloading; String and Array Objects
More about OOP and ADTs Classes
Operator Overloading, Friends, and References
Operator Overloading.
CISC/CMPE320 - Prof. McLeod
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; String and Array Objects
Chapter 18 - Operator Overloading
Operator Overloading II
Jim Fawcett Copyright ©
Presentation transcript:

Operator Overloading I

Operator overloading C++ operators can be overloaded to perform user-defined, object- based functions Type of operators Both unary and binary Assignment Relational Input/output Subscript … + - * / % ^ & | ~ ! , = < > <= >= ++ -- << >> == != && || += -= /= %= ^= &= |= *= <<= >>= [] () -> ->* new new [] delete delete[] C++ operators that can be overloaded

Operator Overloading Define a method/function using the keyword operator Combine the keyword with the operator symbol to be overloaded Ex: bool operator>(int i); This will “compare” an integer to the current object Note: overloading can be defined for different types! For unary operators (!, ++), the overload method should be a member of the class For binary operators (two arguments) the overload method may be a member of the class depending on the “left-hand rule”

“left-hand rule” Applies to binary operators The rule: If the object itself is on the left-hand side of the expression to be evaluated, the operator method can be made a member of the class In this case, only the right hand side of the expression need to be passed to the method, the left hand is implied (it is the class instance itself) If not, operator overloading can still be done, but it must be defined externally to the class In this case, both the left and right hand sides of the expression need to be passed to the overload function (i.e. two arguments)

Applying the “left-hand rule” Left hand side is the object type Compare two Date objects bool operator>(const Date &d) { … } Single argument: the “other” Date object Left hand side is another object type Output a Date object ostream &operator<<( ostream &output, const Date &d ) { … } Two arguments: the stream and the date Example: Date-OpOver1

Terminology For Binary Operators: The right-hand operand is referred to as an rvalue The left-hand operand is referred to as an lvalue

Friends of a class A friend of a class is an object or function that is Not a member of the class, but Is allowed access to the private members of the class anyway A friend is declared in the class definition using the keyword friend friend ostream &operator<<( ostream &, const ClassName& ); // in class definition Function is granted access to private data members Usage of friends, while allowed, is discouraged It violates the basic OOP principle of encapsulation Example: Date-Friend