Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads.

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.
Operator Overloading Fundamentals
Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
Operator Overloading Programming in C++ Fall 2008 Dr. David A. Gaitros
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
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
1 Operator Overloading. 2 Syntax The general syntax is: [friend] returntype operator ( ) { ; }
Chapter 13: Overloading.
Lecture 5 Constructors Operator Overloads “Absolute C++” Chapters 7.1,8.1,8.2.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Operator Overloading and Memory Management. Objectives At the conclusion of this lesson, students should be able to: Overload C++ operators Explain why.
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 1. Introduction Let’s define a class for Complex numbers: class Complex { private: double real, image; public: Complex () : real(0.0),
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 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
CMSC 202 Lesson 12 Operator Overloading II. Warmup  Overload the + operator to add a Passenger to a Car: class Car { public: // some methods private:
1 Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
Operators & Overloading Joe Meehean. Expressions Expression composed of operands combined with operators e.g., a + b Operands variables and literals in.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Operator Overloading ~ and User Defined Conversions.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
C# Operator Overloading and Type Conversions C#.NET Software Development Version 1.0.
CS Object Oriented Programming Using C++
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
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.
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.
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.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
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.
Object Oriented Programming COP3330 / CGS5409.  Arithmetic Operator Overloading  Increment (++) / Decrement (--)  In-class exercise.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
Andy Wang Object Oriented Programming in C++ COP 3330
Operator Overloading Introduction
Overloading C++ supports the concept of overloading Two main types
Operator Overloading.
Chapter 13: Overloading and Templates
Templates.
Visit for more Learning Resources
Object Oriented Programming COP3330 / CGS5409
Jim Fawcett Copyright ©
Chapter 15: Overloading and Templates
Operator Overloading CSCE 121 J. Michael Moore
Overloading the << operator
Operator Overloading.
Friends, Overloaded Operators, and Arrays in Classes
Operator Overloading.
CISC/CMPE320 - Prof. McLeod
COP 3330 Object-oriented Programming in C++
Operator Overloading I
Operator Overloading II
Overloading the << operator
Operator Overloading CSCE 121 Based on Slides created by Carlos Soto.
Jim Fawcett Copyright ©
Presentation transcript:

Operator Overloading

Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads the standard binary operators Explain the rules for operator overloading

The expression a + b * c is simpler to understand then the expression plus(a, times(b, c) )

Therefore, in C++ you can define new meanings for the C++ operators that operate on objects derived from classes that you have defined.

Some Rules: An operator is overloaded by writing a function whose name is the word operator, followed by the symbol for the operator being overloaded. With some exceptions, these operator can be written as member functions or as non-member functions. You cannot change the meaning of any of the C++ operators for built in data types. For example, you cannot change how plus works on integer data. Precedence and associativity are fixed by the language. You cannot change these for an operator.

Examples of Operator Overloading: The Number class

Class Number { public: Number( ); Number(int); int getValue( ); private: int theValue; };

Overloading a Binary Operator As a Member Function

Note that when you write a = b + c; the values of b and c do not change.

When using a member function we will use the following terminology (a, b, and c are objects of the Number class): this is the right hand operand (rho) This is the left hand operand (lho) a = b + c;

Overloading a binary operator as a member function Number Number::operator+(const Number& rho) { int newValue = theValue + rho.getValue( ); Number newNumber(newValue); return newNumber; }

Number Number::operator+(const Number& rho) { int newValue = theValue + rho.getValue( ); Number newNumber(newValue); return newNumber; } Question: Why is this Number object not being returned by reference?

Now, when the compiler sees the statement a = b + c; it generates the code a = b.operator+(c); the message is sent to the left hand operand the right hand operand is passed as the parameter.

Overloading a binary operator as a non-member function Number operator-(const Number& lho, const Number& rho) { int newValue = lho.getValue( ) - rho.getValue( ); Number newNumber(newValue); return newNumber; }

Overloading the Stream Functions

ostream& operator<<(ostream& out, const Number& rho) { out << rho.getValue( ); return out; }

Overloading the Increment Operators

Number& Number::operator++( ) { theValue++; return *this; } Can you explain what happens here? What gets returned? This is the pre-increment operator

To overload the post-increment, we have’ to give the compiler a clue that tells it this is a post increment, not a pre-increment. Also, the code will be much different. Why?

Number Number::operator++(int n) { Number newNumber(theValue); theValue++; return newNumber; } The int parameter tells the compiler that this is a post-increment.

Overloading a Comparison Operator

bool Number::operator==(const Number& rho) { if (theValue == rho.theValue) return true; else return false; }