Operator Overloading Customised behaviour of operators Unit - 06.

Slides:



Advertisements
Similar presentations
Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
Advertisements

Operator overloading redefine the operations of operators
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
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
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
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.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
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 ( ) { ; }
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
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 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++
Review of C++ Programming Part II Sheng-Fang Huang.
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 12: Adding Functionality to Your Classes.
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.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
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.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
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.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
OPERATOR OVERLOADING Customised behaviour of operators.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Inheritance and Composition Reusing the code and functionality Unit - 04.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-1 Concordia University Department of Computer Science and Software Engineering COMP345.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
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.
Classes: Defining Your Own Data Types Basic principles in OOP Define a new data type as a class and use objects of a class Member Functions –Constructors.
CSCI-383 Object-Oriented Programming & Design Lecture 11.
1 CSC241: Object Oriented Programming Lecture No 08.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Operator Overloading.
Operator Overloading Ritika Sharma.
Chapter 13: Overloading and Templates
Introduction Rules Types Programs
Developed By : Ms. K. S. Kotecha
Constructor & Destructor
Operator Overloading BCA Sem III K.I.R.A.S.
group work #hifiTeam
Operator Overloading
Andy Wang Object Oriented Programming in C++ COP 3330
Operator Overloading; String and Array Objects
Overview of C++ Overloading
Constructor Spl member fn auto ini of object
CISC/CMPE320 - Prof. McLeod
Andy Wang Object Oriented Programming in C++ COP 3330
Presentation transcript:

Operator Overloading Customised behaviour of operators Unit - 06

Unit Introduction This unit covers operator overloading

Unit Objectives After covering this unit you will understand… Operator overloading Operator overloading Different types of operator and their overloading Different types of operator and their overloading Operators that cannot be overloaded Operators that cannot be overloaded Inheritance and overloading Inheritance and overloading Automatic type conversion Automatic type conversion

Syntax and Overview Operator overloading used for customised behaviour of different operators Operator overloading used for customised behaviour of different operators Declared using keyword, represents the operator that’s being overloaded Declared using keyword, represents the operator that’s being overloaded

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

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

Types of Operator Unary operator Unary operator Binary operator Binary operator

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

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

Example: Unary Operators (contd.) void main() { UnaryExample object1(10); UnaryExample object1(10); cout << object1++; // overloaded operator results in value // 11 cout << object1++; // overloaded operator results in value // 11}

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, a<=b, a==b)

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

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

Non-Overloadable Operators Operators that can not be overloaded due to safety reasons: Operators that can not be overloaded due to safety reasons: Member Selection ‘.’ operator Member Selection ‘.’ operator Member dereference ‘.*’ operator Member dereference ‘.*’ operator Exponential ‘**’ operator Exponential ‘**’ operator User-defined operators User-defined operators Operator precedence rules Operator precedence rules

Operator Overloading and Inheritance An operator is overloaded in super class but not overloaded in derived class is called non- member operator in derived class An operator is overloaded in super class but not overloaded in derived class is called non- member operator in derived class In above, if operator is also overloaded in derived class it is called member-operator In above, if operator is also overloaded in derived class it is called member-operator = ( ) [ ] –> –>* operators must be member operators = ( ) [ ] –> –>* operators must be member operators Other operators can be non-member operators Other operators can be non-member operators

Automatic Type Conversion Automatic type conversion by the C++ compiler from the type that doesn’t fit, to the type it wants Automatic type conversion by the C++ compiler from the type that doesn’t fit, to the type it wants Two types of conversion: Two types of conversion: Constructor conversion Constructor conversion Operator conversion Operator conversion

Constructor Conversion Constructor having a single argument of another type, results in automatic type conversion by the compiler Constructor having a single argument of another type, results in automatic type conversion by the compiler Prevention of constructor type conversion by use of explicit keyword Prevention of constructor type conversion by use of explicit keyword

Example: Constructor Conversion class One { public: public: One() {} One() {}}; class Two { public: public: Two(const One&) {} Two(const One&) {}}; void f(Two) {} void main() { One one; One one; f(one); // Wants a Two, has a One f(one); // Wants a Two, has a One}

Operator Conversion Create a member function that takes the current type Create a member function that takes the current type Converts it to the desired type using the operator keyword followed by the type you want to convert to Converts it to the desired type using the operator keyword followed by the type you want to convert to Return type is the name of the operator overloaded Return type is the name of the operator overloaded Reflexivity - global overloading instead of member overloading; for code saving Reflexivity - global overloading instead of member overloading; for code saving

Example: Operator Conversion class Three { int m_Data; int m_Data; public: public: Three(int ii = 0, int = 0) : m_Data(ii) {} Three(int ii = 0, int = 0) : m_Data(ii) {}}; class Four { int m_Data; int m_Data; public: public: Four(int x) : m_Data(x) {} Four(int x) : m_Data(x) {} operator Three() const { return Three(m_Data); return Three(m_Data);}}; void g(Three) {}

Example: Operator Conversion (contd.) void main() { Four four(1); Four four(1); g(four); g(four); g(1); // Calls Three(1,0) g(1); // Calls Three(1,0)}

Type Conversion Pitfalls Compiler performs automatic type conversion independently, therefore it may have the following pitfalls: Compiler performs automatic type conversion independently, therefore it may have the following pitfalls: Ambiguity with two classes of same type Ambiguity with two classes of same type Automatic conversion to more than one type - fan- out Automatic conversion to more than one type - fan- out Adds hidden activities (copy-constructor etc) Adds hidden activities (copy-constructor etc)

Unit Summary In this unit you have covered … Operator overloading Operator overloading Different types of operator Different types of operator Operators that cannot be overloaded Operators that cannot be overloaded Inheritance and overloading Inheritance and overloading Automatic type conversion Automatic type conversion