Jim Fawcett Copyright ©

Slides:



Advertisements
Similar presentations
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
Advertisements

Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
CSC241 Object-Oriented Programming (OOP) Lecture No. 9.
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.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Chapter 15: Operator Overloading
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++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
Case Study - Fractions Timothy Budd Oregon State University.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
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.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
LECTURE LECTURE 13 Operator Overloading Textbook p.203—216 Today: const member functions Overloading Operators Postfix/infix increment.
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. Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement.
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.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
Operator Overloading Moshe Fresko Bar-Ilan University Object Oriented Programing
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value –A copy of the arguments’svalue.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
 Binary operators  Unary operators  Conversion Operators  Proxy Classes (simulating a reference) ▪ bitset example  Special operators  Indexing 
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
Object-Oriented Programming (OOP) Lecture No. 16
Andy Wang Object Oriented Programming in C++ COP 3330
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
Operator Overloading.
Chapter 13: Overloading and Templates
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Visit for more Learning Resources
Object Oriented Programming COP3330 / CGS5409
Constructor & Destructor
Object-Oriented Programming (OOP) Lecture No. 16
Chapter 15: Overloading and Templates
LEC Default Function Arguments, Ambiguity in Function Overloading and Operator Overloading.
Operators Lecture 10 Fri, Feb 8, 2008.
Overloading the << operator
Operator Overloading; String and Array Objects
Operator Overloading.
Advanced Program Design with C++
Operator Overloading; String and Array Objects
Operator overloading Dr. Bhargavi Goswami
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++
Data Structures and Algorithms Introduction to Pointers
Operator Overloading I
Operator Overloading; String and Array Objects
Overloading the << operator
Jim Fawcett CSE687 – Object Oriented Design Spring 2002
Jim Fawcett Copyright ©
Presentation transcript:

Jim Fawcett Copyright © 2002-2006 CSE687 – Object Oriented Design Class Notes C++ Operators Jim Fawcett Copyright © 2002-2006

C++ Binary Operator Model A C++ operator is really just a function. Assignment, for example, may be invoked either way shown below: x = y; or x.operator=(y); Here, the x object is invoking the assignment operator on itself, using y for the assigned values. The left hand operand is always the invoking object and the right hand operand is always passed to the function as an argument. General form of the binary operator: x@y  x.operator@(y) - member function x@y  operator@(x,y) - global function

Indexing Operators Indexing operators should usually come in pairs: val& X::operator[](int n); x[3] = ‘a’; val X::operator[](int n) const; char ch = x[2]; The second form allows you to pass an indexed object into a function by const reference and still be able to read indexed values. With only the first form, any indexing in the function will result in a compile time error since the operator does not guarantee not to change the const object.

Unary Increment/Decrement Operators This example based on iterators pointing to contiguous memory iterator& operator++() { /* ++(this->ptr); return *this */ } iterator operator++(int) { /* iterator temp = *this, ++(this->ptr), return temp */ } iterator& operator--() { /* --(this->ptr); return *this */ } iterator operator--(int) { /* iterator temp = *this; --(this->ptr), return temp */ }

Sum Operators Arithmetic operators should come in pairs. Addition looks like this: X& X::operator+=(const X &x); X X::operator+(const X &x); Addition should be implemented this way: X X::operator+(const X &x) { X temp = *this; // copy of me temp += x; // copy of me + x return temp; } You implement operator+=(…) first, and get operator+(…) almost for free.

Overloading Arithmetic Operators Define: operator+, operator-, operator*, and operator/ in terms of : operator+=, operator-=, operator*=, and operator/= Remember the binary operator model: operators as class members: x@y  x.operator@(y) operators as global functions: x@Y  operator(x,y)

Insertion The insertion and extraction operators: ostream& operator(ostream& out, const X &x); istream& operator(ostream& in, const X &x); Have to be implemented as global (non-member) functions since they are invoked with the statements: out << x; and in >> x; Since the streams, out and in, appear on the left side of the operator, and are not objects of the X class, we must use the global form shown at the top of this slide. You should try to implement them without making them friends of the X class. You may need to implement public helper functions to do that.

End of Presentation