1 Operator Overloading. 2 Syntax The general syntax is: [friend] returntype operator ( ) { ; }

Slides:



Advertisements
Similar presentations
Operator Overloading Fundamentals
Advertisements

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
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
More on Operator Overloading CS-2303, C-Term More on Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The.
Chapter 13: Overloading.
Chapter 15: Operator Overloading
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,
Operator overloading Object Oriented Programming.
Operator Overloading in 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.
Operator Overloading and Type Conversions
Operator Overloading Customised behaviour of operators Unit - 06.
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.
CMSC 202 Lesson 12 Operator Overloading II. Warmup  Overload the + operator to add a Passenger to a Car: class Car { public: // some methods private:
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Operator Overloading ~ and User Defined Conversions.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4.
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.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
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.
Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
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.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
C++ 程序语言设计 Chapter 11: Operator Overloading. Outline  Operator overloading  Using of friends  Overload “
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
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
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.
CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
1 CSC241: Object Oriented Programming Lecture No 08.
 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++)
Programming with ANSI C ++
CSE1002 – Problem Solving with Object Oriented Programming
Learning Objectives Pointers as dada members
Chapter 13: Overloading and Templates
Object Oriented Programming COP3330 / CGS5409
Constructor & Destructor
Chapter 15: Overloading and Templates
Operator Overloading; String and Array Objects
Andy Wang Object Oriented Programming in C++ COP 3330
Overview of C++ Overloading
Operator overloading Dr. Bhargavi Goswami
CISC/CMPE320 - Prof. McLeod
Operator Overloading; String and Array Objects
Andy Wang Object Oriented Programming in C++ COP 3330
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
SPL – PS3 C++ Classes.
Presentation transcript:

1 Operator Overloading

2 Syntax The general syntax is: [friend] returntype operator ( ) { ; }

3 Syntax  The number of arguments in the overloaded operator’s argument list depends on two factors:  Whether it’s a unary operator (one argument) or a binary operator (two arguments).  Whether the operator is defined as a global function (one argument for unary, two for binary) or a member function (zero arguments for unary, one for binary – the object becomes the left-hand argument).

4 Operators that can’t be overloaded  All operators can be overloaded except for the ones below:  The member selection operator -.  The pointer to member dereference operator -.*  Scope access/resolution operator - ::  Conditional operator - ?:  Also, there are no user-defined operators and you can’t change the precedence rules.

5 Overloaded operators as member or non-member functions?  The decision is based on what argument(s) is needed by the operator.  In general, if it doesn’t make any difference, they should be members, to emphasize the association between the operator and its class.  When the left-hand operand is always an object of the current class, this works fine.  Sometimes you want the left-hand operand to be an object of some other class, then the overloaded operator cannot be a member function.

6 Some ”tricky” operators  Increment and decrement operators  ++a (a pre-increment) generates a call to operator++(a);  a++ (a post-increment) generates a call to operator++(a,int) for non-member functions.  The member function versions, would be:  B::operator++( ); and  B::operator++(int);  Note: a dummy value is passed by the compiler to distinguish (generate different signatures) the two versions.

7 Some ”tricky” operators  The assignment operator  Defining the assignment operator has a lot in common with defining the copy constructor and the destructor. List& List::operator=( const List& b ) { if( this != &b) //check for self assignment { free(); copy(b); } return *this; }

8 An example  Let’s look at our example implementation of the class ’Values’

9 Arguments and return values  Guidelines  Return by value as const  The return value optimization