INTERNATIONAL INSTITUTE OF IFORMATION TECHNOLOGY, (I²IT)

Slides:



Advertisements
Similar presentations
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Advertisements

I NTRODUCING O PERATOR O VERLOADING Chapter 6 Department of CSE, BUET 1.
Introduction to Programming Lecture 31. Operator Overloading.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 14: Overloading and Templates
Chapter 13: Overloading.
Chapter 15: Operator Overloading
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.
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 and type convesions BCAS,Bapatla B.mohini devi.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
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.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Programming with ANSI C ++
Operator Overloading.
Object-Oriented Programming (OOP) Lecture No. 16
Constructors and Destructors
Operator Overloading Ritika Sharma.
Chapter 13: Overloading and Templates
Constructor & Destructor
Object-Oriented Programming (OOP) Lecture No. 16
Chapter 15: Overloading and Templates
Operator Overloading BCA Sem III K.I.R.A.S.
Operator Overloading
Operators Lecture 10 Fri, Feb 8, 2008.
Constructor Spl member fn auto ini of object
Constructors and Destructors
Operator overloading Dr. Bhargavi Goswami
Operator Overloading.
COMPUTER NETWORK TECHNOLOGY
DESIGN PATTERNS : State Pattern
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Physics of Nanoparticles
Composition & Resolution of Forces
DESIGN PATTERNS : Adapter Pattern
EQUIVALENCE CALCULATIONS UNDER INFLATION
NP-COMPLETE Prof. Manjusha Amritkar Assistant Professor Department of Information Technology Hope Foundation’s International Institute of Information.
International Institute of Information Technology, (I²IT).
Engineering Mathematics-I Eigenvalues and Eigenvectors
Pass Structure of Assembler
INTERNATIONAL INSTITUTE OF IFORMATION TECHNOLOGY, (I²IT)
ADDITION OF TWO POLYNOMIALS
Newton’s Laws of Motion
By Sandeep Patil, Department of Computer Engineering, I²IT
Differential Equation
Chapter 6 Polymorphism.
BINARY HEAP Prof ajitkumar shitole Assistant Professor Department of computer engineering Hope Foundation’s International Institute of Information.
gyroscope Prof Varsha Degaonkar Assistant Professor
Hope Foundation’s International Institute of Information.
Essay Writing Writing is an art Prepared by Vaidehi Banerjee
Introduction to Human Computer Interaction
Design procedure for an Integrator
Basic Electrical Engg. UNIT:-IV AC Fundamental
Tel Hope Foundation’s International Institute of Information Technology, (I²IT). Tel
PIC Microcontroller ADC interfacing Prof. Ashvini Kulkarni
INTERNATIONAL INSTITUTE OF IFORMATION TECHNOLOGY, (I²IT)
DAA - Introduction Dr. Sashikala Mishra Associate Professor Department of Computer Engineering Hope Foundation’s INTERNATIONAL INSTITUTE OF INFORMATION.
Tel Hope Foundation’s International Institute of Information Technology, (I²IT). Tel
Transaction Serializability
CASCADING STYLE SHEET WEB TECHNOLOGY
Presentation transcript:

INTERNATIONAL INSTITUTE OF IFORMATION TECHNOLOGY, (I²IT) HOPE FOUNDATION’S INTERNATIONAL INSTITUTE OF IFORMATION TECHNOLOGY, (I²IT) www.isquareit.edu.in +91 20 22933441 / 2

Operator Overloading Definition C++ tries to make the user-defined data types behave in much the same way as the built-in types. For instance, C++ permits us to add two variables of user-defined types with the same syntax that is applied to the basic types. This means that C++ has the ability to provide the operators with special meaning for a data type. The mechanism of giving such special meaning to an operator is known as operator overloading. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in

Operator Overloading We can overload all the all the C++ operators except the following: Class member access operators (.,.*) Scope resolution operator (::) Size operator (sizeof) Conditional operator (?:) Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in

Operator Overloading Syntax: Return type classname::operator (op-arglist) { Function body //task definition } Operator function must be either member function (non static) or friend function. Friend function will have only one argument for unary operators and two for binary operators. Member function has no argument for unary operators and only one for binary operators. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in

Operator Overloading Overloading unary operator: Following program shows how the unary minus operator overload: void operator – (); //Function declaration -s; //Function calling // s is object of class space void space::operator-() // Function definition { x=-x; // x is a data member of class space } Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in

Operator Overloading Following program shows how the unary minus operator overload when operator function is friend function. friend void operator – (space); //Function declaration -s; //Function calling // s is object of class space void operator-(space &a) // Function definition { a.x=-a.x; // x is a data member of class space } Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in

Operator Overloading Following program shows how the binary + operator overload when operator function is a member function: complex operator +(complex); //Function declaration c3=c1+c2; //Function calling // c1,c2 and c3 are the objects of class complex // c3=c1.operator+(c2) complex complex::operator+(complex x) // Function definition { complex temp; temp.real=real+x.real //real and img are data members of class complex temp.img=img+x.img; return temp; } Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in

Operator Overloading Following program shows how the binary + operator overload when operator function is a friend function: friend complex operator +(complex,complex); //Function declaration c3=c1+c2; //Function calling // c1,c2 and c3 are the objects of class complex // c3=operator+(c1,c2) complex operator+(complex x,complex y) // Function definition { complex temp; temp.real=x.real+y.real; //real and img are data members of class complex temp.img=x.img+y..img; return temp; } Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in

Operator Overloading Rules for Overloading Operators: Only existing operators can be overloaded. New operators can not be created. The overloaded operator must have at least one operant that is user defined type. We cannot change the basic meaning of an operator. Unary operators, overloaded by means of member function, take no explicit arguments and return no explicit values, but, those overloaded by means of a friend function, take one reference argument (the object of relevant class). Binary operators overloaded through member function take one explicit argument and those which are overloaded through a friend function take two explicit argument. When using binary operators overloaded through a member function, the left hand operand must be an object of the relevant class. Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in

Operator Overloading Rules for Overloading Operators: Binary arithmetic operators such as +,-,* and / must explicitly return a value. They must not attempt to change their own arguments. Overloaded operators follow the syntax rules of the original operators. They cannot be overridden. We can not use friend functions to overload following operators. Assignment operator = Function call operator () Subscripting operator [] Class member access operator -> Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in

References E Balgurusamy, “Object-Oriented Programming with C++”, Second Edition, Tata Mcgraw Hill 2. Herbert Schildt, “C++: The Complete Reference”, 4th Edition, Tata Mcgraw Hill 3. https://www.tutorialspoint.com/cplusplus/cpp_overloading.htm Hope Foundation’s International Institute of Information Technology, I²IT, P-14 Rajiv Gandhi Infotech Park, Hinjawadi, Pune - 411 057 Tel - +91 20 22933441 / 2 / 3 | Website - www.isquareit.edu.in ; Email - info@isquareit.edu.in

THANK YOU For further information please contact Prof. Sarang A.Saoji Department of Information Technology Hope Foundation’s International Institute of Information Technology, I²IT Hinjawadi, Pune – 411 057 Phone - +91 20 22933441 www.isquareit.edu.in | sarangs@isquareit.edu.in