Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.

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
(10-1) OOP: Inheritance in C++ D & D Chapter 20 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
CS-240 Operator Overloading Dick Steflik. Operator Overloading What is it? –assigning a new meaning to a specific operator when used in the context of.
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.
Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.
Recursion (II) H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 25, 2011) Washington State University.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
© 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
(1 - 2) Abstract Data Types D & D Chapter 12 Instructor - Andrew S. O’Fallon CptS 122 (June 10, 2015) Washington State University.
Chapter 13: Overloading.
Chapter 15: Operator Overloading
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++
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Chapter 14 – Object Oriented Design. Copy Constructor u Called with an object as an argument u General declarator Class :: Class (Class& alias) –Class.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Overloading Operators. Operators  Operators are functions, but with a different kind of name – a symbol.  Functions.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
(1 - 1) Introduction to C Data Structures & Abstract Data Types Instructor - Andrew S. O’Fallon CptS 122 (August 26, 2015) Washington State University.
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.
Operator overloading and type convesions BCAS,Bapatla B.mohini devi.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
(5-1) Selection Structures III in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 21, 2015) Washington State University.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University.
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,
CONSTRUCTOR AND DESTRUCTORS
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 2009.
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
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.
Friend Function. 2 Any data which is declared private inside a class is not accessible from outside the class. A non-member function cannot have an access.
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.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Object-Oriented Programming (OOP) and C++
Classes: A Deeper Look D & D Chapter 9 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Operator.
(1-3) Basics of a Linked List I Instructor - Andrew S. O’Fallon CptS 122 (June 9, 2016) Washington State University.
Object Oriented Programming COP3330 / CGS5409.  Arithmetic Operator Overloading  Increment (++) / Decrement (--)  In-class exercise.
Operator Overloading.
Yan Shi CS/SE 2630 Lecture Notes
Chapter 13: Overloading and Templates
Overloading Operator MySting Example
Object Oriented Programming COP3330 / CGS5409
Chapter 15: Overloading and Templates
(2-1) Data Structures & The Basics of a Linked List I
(5 - 1) Object-Oriented Programming (OOP) and C++
(2-1) Data Structures & The Basics of a Linked List I
Operator overloading Dr. Bhargavi Goswami
(5 - 1) Object-Oriented Programming (OOP) and C++
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
(1 - 2) Introduction to C Data Structures & Abstract Data Types
(4 – 2) Introduction to Classes in C++
Presentation transcript:

Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University

Key Concepts Keyword operator Operator overloading A. O’Fallon, J. Hagemeister 2

What is Operator Overloading? A generalization of function overloading An extension to C++ standard operators to define how they should work with user- defined types such as classes A. O’Fallon, J. Hagemeister 3

Why Overload Operators? Improves readability Allows for a more natural way to implement code A. O’Fallon, J. Hagemeister 4

Rules and Restrictions on Operator Overloading The precedence of an operator cannot be changed The associativity of an operator cannot be changed, i.e. left-to-right or right-to-left The “arity” of an operator cannot be changed, i.e. if the operator accepts one operand (unary) or two operands (binary) Only existing operators may be overloaded A. O’Fallon, J. Hagemeister 5

Which Operators Cannot be Overloaded?..* (pointer to member) :: ?: A. O’Fallon, J. Hagemeister 6

Recall Class ComplexNumber’s Add () Function Let’s write the definition for the add() member function // Prototype: ComplexNumber add (const ComplexNumber &operand); // Definition – notice the binary scope resolution operator ComplexNumber ComplexNumber::add (const ComplexNumber &operand) { // This adds the real part of the “operand” object // to the real part of the object that invokes the // call to the function; it also adds the imaginary // parts ComplexNumber result; // Declare local ComplexNumber // Recall we use the dot member operator (.) to access // members of a class; no dot (.) denotes accessing the // instantiated object’s members; note we don’t have to apply “special” // operators to access an object passed by reference! result.mRealPart = mRealPart + operand.mRealPart; result.mImaginaryPart = mImaginaryPart + operand.mImaginaryPart; // Don’t want to pass back by reference; cause undefined behavior return result; } 7

We Can Replace Add () by Overloading + Let’s write a function to overload the binary + operator; this function is a non-member function // Prototype: friend ComplexNumber operator+ (const ComplexNumber &lhs, const ComplexNumber &rhs); // Definition – notice the operator is not preceded by ComplexNumber::, because // it’s a non-member operator. Made this function a friend of ComplexNumber // to efficiently access the private data members of ComplexNumber. ComplexNumber operator+ (const ComplexNumber &lhs, const ComplexNumber &rhs) { ComplexNumber result; // Declare local ComplexNumber result.mRealPart = lhs.mRealPart + rhs.mRealPart; result.mImaginaryPart = lhs.mImaginaryPart + rhs.mImaginaryPart; // Don’t want to pass back by reference; cause undefined behavior return result; } A. O’Fallon, J. Hagemeister 8

Why Non-Member Overloaded Operators? Enables “symmetry” and communitivity among operators, i.e. – ComplexNumber operator+ (const ComplexNumber &lhs, int rhs); – ComplexNumber operator+ (int lhs, const ComplexNumber &rhs); Important to make non-member operator+ when lhs is not a class! Since lhs is not an object in this case! A. O’Fallon, J. Hagemeister 9

10 References P.J. Deitel & H.M. Deitel, C++: How to Program (9th ed.), Prentice Hall, 2014 J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (7 th Ed.), Addison- Wesley, 2013

A. O’Fallon, J. Hagemeister 11 Collaborators Jack Hagemeister