Operator Overloading Ritika Sharma.

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
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.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
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++
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
Operator Overloading Customised behaviour of operators Unit - 06.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler.
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.
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.
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.
OPERATOR OVERLOADING Customised behaviour of operators.
Operator Overloading. Introduction Computer is calculating machine. It calculates the data provided to it. It performs the various operations on data.
CONSTRUCTOR AND DESTRUCTORS
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 2009.
Chapter -6 Polymorphism
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.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Operator Overloading.
Object-Oriented Programming (OOP) Lecture No. 16
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading.
Chapter 13: Overloading and Templates
Chapter 7: Expressions and Assignment Statements
Object Oriented Programming COP3330 / CGS5409
Operator Overloading.
Developed By : Ms. K. S. Kotecha
Polymorphism in C++ Operator Overloading
Object-Oriented Programming (OOP) Lecture No. 21
Chapter 7: Expressions and Assignment Statements
Objectives Define polymorphism Overload functions
Constructor & Destructor
Object-Oriented Programming (OOP) Lecture No. 16
Chapter 15: Overloading and Templates
Operator Overloading BCA Sem III K.I.R.A.S.
Operators and Expressions
LEC Default Function Arguments, Ambiguity in Function Overloading and Operator Overloading.
Operator Overloading
Operators Lecture 10 Fri, Feb 8, 2008.
Operator Overloading; String and Array Objects
Operator Overloading.
Operator Overloading; String and Array Objects
Constructor Spl member fn auto ini of object
Operator overloading Dr. Bhargavi Goswami
Operator Overloading.
POLYMORPHISM ( in C++) POLYMORPHISM ( in C++). Presentation Outline Polymorphism Definition Types – Compile time and Run time polymorphism Function overloading.
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
COP 3330 Object-oriented Programming in C++
INTERNATIONAL INSTITUTE OF IFORMATION TECHNOLOGY, (I²IT)
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Operator Overloading; String and Array Objects
Chapter 6 Polymorphism.
Presentation transcript:

Operator Overloading Ritika Sharma

Introduction Polymorphism is one of the crucial feature of OOP. It simply means ‘one name many forms’. The concept of polymorphism is already implemented using overloaded function and operator. Ritika Sharma

Operator Overloading is a specific case of polymorphism in which some or all of operators like +, =, or == have different implementations depending on the types of their arguments. Ritika Sharma

Operators overloading in C++: You can redefine or overload most of the built-in operators available in C++. Thus a programmer can use operators with user-defined types as well. Overloaded operators are functions with special names the keyword operator followed by the symbol for the operator being defined. Like any other function, an overloaded operator has a return type and a parameter list. Ritika Sharma

Student Book Syntax and Overview Operator overloading used for customised behaviour of different operators Return-type classname :: operator op(args) { //FUNCTION BODY } Operator overloading is used for customised functionality of a an operator in a class. This a powerful tool in C++, where hundreds of lines of code can be slashed, if operator overloaded it done properly and efficiently. Ritika Sharma

Fundamentals of Operator Overloading Types Built in (int, char) or user-defined Can use existing operators with user-defined types Cannot create new operators Overloading operators Create a function for the class Name function operator followed by symbol Operator+ for the addition operator + Ritika Sharma

Operator function must be either member function or friend function. Friend function will have only one argument for unary operators and two for binary operators. Member function will have no arguments for unary and one argument for binary operators. Ritika Sharma

example Suppose vector is a classname Vector operator+(vector); Int operator==(vector); Friend vector operator+(vector, vector) Friend int operator ==(vector,vector) Ritika Sharma

Operator Overloading C++ has the ability to provide the operators with the special meaning for a data type. The mechanism of giving such special meanings to an operator is known as Operator overloading. It provides a flexible option for creation of new definitions for most of C++ operators. Operator Overloading is a specific case of polymorphism in which some or all of operators like +, =, or == have different implementations depending on the types of their arguments. Ritika Sharma

Contt…. We can overload all C++ operators except the following: Class member access operator(.,.*). Scope resolution operators(::); Size operator( sizeof). Conditional operators(?:) Ritika Sharma

Restrictions on Operator Overloading Ritika Sharma

Fundamentals of Operator Overloading Types Built in (int, char) or user-defined Can use existing operators with user-defined types Cannot create new operators Semantics of an operator can be extended but syntax cannot be changed. Overloading operators Create a function for the class Name function operator followed by symbol Operator+ for the addition operator + Ritika Sharma

Restrictions on Operator Overloading Cannot change How operators act on built-in data types i.e., cannot change integer addition When an operator is overloaded original meaning is not changed. For eg: + operator can be used to add two vectors but it can also used to add two integers. Precedence of operator (order of evaluation) Use parentheses to force order-of-operations Associativity (left-to-right or right-to-left) Number of operands & is unitary, only acts on one operand Ritika Sharma

Restrictions on Operator Overloading Cannot create new operators Operators must be overloaded explicitly Overloading + does not overload += Ritika Sharma

Syntax return_type operator # (argument list) {………..} Eg- Complex operator +(complex); Void operator >(distance); Ritika Sharma

Function used for this is called operator function. First of all we must specify the class to which the operator is applied. Function used for this is called operator function. return-type classname:: operator op(arglist) { function body; } Ritika Sharma

Steps in process of overloading Create a class that defines data types that is to be used in the overloading operation. Declare the operator function operator op() in public part of the class. Define the function to implement the required operation. Ritika Sharma

Types of Operator Unary operator Binary operator Ritika Sharma Student Book Types of Operator Unary operator Binary operator Ritika Sharma

Student Book Unary Operators Operators attached to a single operand (-a, +a, --a, a--, ++a, a++) The pre and post increment and decrement operators and overloading in different ways. This is because they have a different effect on objects and their values. e.g. a=14; cout << a++; // will print 14 and increment a cout << ++a; // will increment a and print 15 Ritika Sharma

Prefix unary operators Simple Prefix Unary Operators Are defined by either a member function that takes no parameter or a non-member function that takes one parameter Example: i1.operator -() or operator -(i1) -i1 Ritika Sharma

class space { private: int x; int y; int z; public: space(int a, int b, int c) { x=a; y=b; z=c; } Ritika Sharma

void display() { cout<<<x; cout<<<y; cout<<<"\n"; } void operator-() { x=-x; y=-y; z=-z; } }; Ritika Sharma

void main() { clrscr(); space s(10,-20,30); cout<<"s :"; s void main() { clrscr(); space s(10,-20,30); cout<<"s :"; s.display(); -s; cout<<"s :"; s.display(); getch(); } Ritika Sharma

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

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

Friend function friend void operator –(space &s); //declaration void operator –(space &s) { s.x=-s.x; s.y=-s.y; s.z=-s.z; } Ritika Sharma

Student Book 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) Ritika Sharma

Example: Binary Operators (contd.) Student Book Example: Binary Operators (contd.) void main() { BinaryExample object1(10), object2(20); cout << object1 + object2; // overloaded operator called } ob1 = ob1 + ob2; equivalent to ob1 = ob1.operator+(ob2); Ritika Sharma

complex operator -(complex c) { complex temp; temp.x= x- c.x; Eg:- class complex { float x; float y; public: complex(){ x=10;y=20;} complex operator -(complex c) { complex temp; temp.x= x- c.x; temp.y=y-c.y; return(temp); } Void show() Cout<<x<<y; }}; Ritika Sharma

Example: Binary Operators (contd.) void main() { complex C1,C2,C3; C3=C1-C2; C3.show(); } Ritika Sharma

Rules for overloading operators. Only existing operators can be overloaded. New operators cannot be created. The overloaded operator must have at least one operand that is of user defined type. We cannot change the basic meaning of the operator. Overloaded operators follow the syntax rules of the original operators. They cannot be overridden. Ritika Sharma

Rules for overloading operators. There are some operators that cannot be overloaded. We cannot use friend functions to overload certain operators. Member functions can be used to overload them. Unary operators overloaded by means of member function, take no explicit arguments and return no explicit values. Ritika Sharma

Rules for overloading operators. Binary operators overloaded through a member function take two explicit arguments. When using binary operators overloaded through a member function, the left hand operand must be an object of the relevant class. Ritika Sharma