OPERATOR OVERLOADING Customised behaviour of operators.

Slides:



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

Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
I NTRODUCING O PERATOR O VERLOADING Chapter 6 Department of CSE, BUET 1.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Operator Overloading Fundamentals
Class and Objects.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
 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.
Chapter 14: Overloading and Templates
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.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
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,
Operator overloading Object Oriented Programming.
Operator Overloading in C++
1 CSC241: Object Oriented Programming Lecture No 07.
Review of C++ Programming Part II Sheng-Fang Huang.
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.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
Operator Overloading Customised behaviour of operators Unit - 06.
1 Operator Overloading in C++ Copyright Kip Irvine, All rights reserved. Only students enrolled in COP 4338 at Florida International University may.
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.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
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 in C++. Operator Overloading It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
CS Object Oriented Programming Using C++
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 2009.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
1 Lecture 17 Operator Overloading. 2 Introduction A number of predefined operators can be applied to the built- in standard types. These operators can.
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.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
1 CSC241: Object Oriented Programming Lecture No 08.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Dale Roberts 1 Operator Overloading Member Functions Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
Operator Overloading.
Chapter 18 - C++ Operator Overloading
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading Ritika Sharma.
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Operator Overloading; String and Array Objects
Overloading Operator MySting Example
Introduction Rules Types Programs
Developed By : Ms. K. S. Kotecha
Objectives Define polymorphism Overload functions
Operator Overloading; String and Array Objects
Operator Overloading.
Operator Overloading; String and Array Objects
Constructor Spl member fn auto ini of object
Operator Overloading.
Operator Overloading.
COP 3330 Object-oriented Programming in C++
Operator Overloading; String and Array Objects
Chapter 18 - Operator Overloading
Presentation transcript:

OPERATOR OVERLOADING Customised behaviour of operators

Introduction  Operator overloading  Use traditional operators with user-defined objects  Straightforward and natural way to extend C++  Requires great care When overloading misused, program difficult to understand

Fundamentals of Operator Overloading  Use operator overloading to improve readability  Avoid excessive or inconsistent usage  Format  Write function definition as normal  Function name is keyword operator followed by the symbol for the operator being overloaded.  operator+ would be used to overload the addition operator ( + )

Restrictions on Operator Overloading  Most of C++’s operators can be overloaded

Restrictions on Operator Overloading (II)  Arity (number of operands) cannot be changed  Urnary operators remain urnary, and binary operators remain binary  Operators *, + and - each have unary and binary versions Unary and binary versions can be overloaded separately

Restrictions on Operator Overloading (III)  No new operators can be created  Use only existing operators  Built-in types  Cannot overload operators  You cannot change how two integers are added

Coding Practices

Example: Operator Overloading class OverloadingExample { private: int m_LocalInt; public: OverloadingExample(int j) // default constructor { m_LocalInt = j; } int operator+ (int j) // overloaded + operator { return (m_LocalInt + j); } };

Example: Operator Overloading (contd.) void main() { OverloadingExample object1(10); cout << object1 + 10; }

Types of Operator  Unary operator  Binary operator

Unary Operators  Operators attached to a single operand (-a, +a, -- a, a--, ++a, a++)

Example: Unary Operators class UnaryExample { private: int m_LocalInt; public: UnaryExample(int j) { m_LocalInt = j; } int operator++ () { return (m_LocalInt++); } };

Example: Unary Operators (contd.) void main() { UnaryExample object1(10); cout << object1++; }

Prefix and Postfix Notation int operator++ () // Prefix { return (++m_LocalInt); } int operator++ (int) // postfix { return (m_LocalInt++); } Only difference is the int in the parenthesis

Binary Operators  Operators attached to two operands a+b, a-b, a*b etc.)

Example: Unary Operators class UnaryExample { private: int m_LocalInt; public: UnaryExample(int j) { m_LocalInt = j; } int operator++ () { return (m_LocalInt++); } };

Binary Operators  Operators attached to two operands (a-b, a+b, a*b, a/b, a%b, a>b, a>=b, a<b, a<=b, a==b)

Example: Binary Operators class BinaryExample { private: int m_LocalInt; public: BinaryExample(int j) { m_LocalInt = j; } int operator+ (BinaryExample rhsObj) { return (m_LocalInt + rhsObj.m_LocalInt); } };

Example: Binary Operators (contd.) void main() { BinaryExample object1(10), object2(20); cout << object1 + object2; }

Another Way of Doing it! How to solve object1 + object2 +object3+……?

class BinaryExampleU { private: int m_LocalInt; public: BinaryExampleU(int j) { m_LocalInt = j; } BinaryExampleU operator+ (BinaryExampleU rhsObj) { return BinaryExampleU(m_LocalInt + rhsObj.m_LocalInt); } void print() { cout<<m_LocalInt; } };

void main() { BinaryExampleU object1(10), object2(20), object3 (30), object4(0); object4 = object1 + object2 +object3 ; object4.print(); }

Case Study: An Array class  Implement an Array class with  Range checking  Array assignment  Arrays that know their size  Outputting/inputting entire arrays with >  Array comparisons with == and !=

Thankyou