Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
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.
Introduction to Programming Lecture 31. Operator Overloading.
Operator Overloading Fundamentals
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.
Chapter 14: Overloading and Templates
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
Chapter 13: Overloading.
Chapter 15: Operator Overloading
Plab – Exercise 5 C++: references, operator overloading, friends.
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. 2 C++ has the ability to assign special meaning to an operator, called operator overloading one operator can be used to carry out.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Operator Overloading and Type Conversions
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.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
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.
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 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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 8.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Dynamic object creation
Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
©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();
LECTURE LECTURE 13 Operator Overloading Textbook p.203—216 Today: const member functions Overloading Operators Postfix/infix increment.
Operator Overloading. Introduction Computer is calculating machine. It calculates the data provided to it. It performs the various operations on data.
CONSTRUCTOR AND DESTRUCTORS
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Overloading operators C++ incorporates the option to use standard operators to perform operations with.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.
CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I.
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.
Text Overloading Techniques –Using CPP. 2 Overloading Techniques -Agenda 1.Introduction to Overloading 2.Function Overloading 3.Constructor Overloading.
Constructor It is a special member of a class that has the following characteristic 1)It has the same name as of its class. 2)It don’t have an explicit.
CSCI-383 Object-Oriented Programming & Design Lecture 11.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Constructors & Destructors Controlling initialization & destruction.
Programming with ANSI C ++
Operator Overloading.
Object-Oriented Programming (OOP) Lecture No. 16
Overloading C++ supports the concept of overloading Two main types
Operator Overloading Ritika Sharma.
Operator Overloading.
Chapter 13: Overloading and Templates
Developed By : Ms. K. S. Kotecha
Constructor & Destructor
Object-Oriented Programming (OOP) Lecture No. 16
Chapter 15: Overloading and Templates
Operator Overloading BCA Sem III K.I.R.A.S.
LEC Default Function Arguments, Ambiguity in Function Overloading and Operator Overloading.
Operator Overloading
Operator overloading Dr. Bhargavi Goswami
Operator Overloading.
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Data Structures and Algorithms Introduction to Pointers
INTERNATIONAL INSTITUTE OF IFORMATION TECHNOLOGY, (I²IT)
Presentation transcript:

Operator Overloading

Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can add two user defined data types such as, objects, with the same syntax, just as basic data types. We can overload almost all the C++ operators except the following: Class member access operators(.,.*) Scope resolution operator (::) Size operator (sizeof) Conditional operator (?:)

Defining Operator Overloading It is done with the help of a special function, called operator function, which describes the special task to an operator. General form: return type classname :: operator op (argulist) { function body//task defined } where return type  type of value returned by the specified operation & op  operator being overloaded. operator op  function name

Steps : Create a class that defines the data type i.e. to be used in the overloading operation. Declare the operator function operator op() in the public part of the class. Note: It may be either a member function or a friend function. Define the operator function to implement the required operations.

Rules for overloading operators Only existing operators can be overloaded. New operators cannot be created. At least one operand (user defined type). Cannot change the basic meaning of the operator. Follow the syntax rules of the original operators. Some operators cannot be overloaded. Cannot use friend function to overload certain operators. When using binary operators overloaded through a member function, the left hand operand must be an object of the relevant class. Binary arithmetic operators must explicitly return a value.

Not used / overloaded Operators that cannot Where a friend be overloaded function can’t be used Sizeof = (assignment). (membership operator) ( ) (function call).* (pointer to member operator) [ ] (subscripting) ::  (class member access) ?:

Overloading of Unary Operators ++ & -- Operator Unary – Operator >> & << Operator Note: - already discussed in the lab.

Overloading of Binary Operators Arithmetic operators (+, -, *, /) Relational Operators (, ==, =, !=)

Program: - Arithmetic Operator class complex { float x,y; public: complex() { } int main() complex(float real, float img) { { x = real; y = img;} complex c1, c2, c3; complex operator + (complex); c1 = complex(2.5, 3.5); void display(void); } ; c2 = complex(1.5, 2.5); complex complex :: operator + (complex c) c3 = c1 + c2; { complex temp; cout<< “c1”; c1.display(); temp.x = x + c.x; cout<< “c2”; c2.display(); temp.y = y + c.y; cout<< “c3”; c3.display(); return (temp);}return 0 ; void complex :: display (void) } { cout << x << “ +I ” << y << endl; }

Explain coding complex complex :: operator + (complex c) { complex temp; temp.x = x + c.x; temp.y = y + c.y; return (temp);} Features: - 1. it receives only one complex type argument explicitly 2. It returns a complex type value. 3. It is a member function of complex.

Overloading Binary Operators Using Friends Friend functions may be used in the place of member functions for overloading a binary operator. Difference : - a friend function requires two arguments to be explicitly passed to it, while a member function requires only one.

Using Friend Function Replace the member function declaration by the friend function declaration. friend complex operator + (complex, complex); Redefine the operator function as follows: complex operator + (complex a, complex b) { return complex ((a.x + b.x), (a.y + b.y)); } In this case, the statement c3 = c1 + c2; is equivalent to c3 = operator + (c1, c2);

Thank You!!!