Introduction Rules Types Programs

Slides:



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

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Template Implicit function overload. Function overload Function overload double ssqq(double & a, double & b) { return(a*b);} float ssqq(float & a, float.
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
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 18 - C++ Operator Overloading Outline 18.1Introduction.
1 Operator Overloading. 2 Syntax The general syntax is: [friend] returntype operator ( ) { ; }
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
OOP Egar 2008 – Recitation 41 Object Oriented Programming Spring 2006 Recitation 6.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Operator Overloading CS 308 – Data Structures What is operator overloading? Changing the definition of an operator so it can be applied on the objects.
Operator overloading Object Oriented Programming.
1 CSC241: Object Oriented Programming Lecture No 07.
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.
Operator Overloading Customised behaviour of operators Unit - 06.
Overloading Operators. Operators  Operators are functions, but with a different kind of name – a symbol.  Functions.
1 Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
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.
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.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
OPERATOR OVERLOADING Customised behaviour of operators.
CONSTRUCTOR AND DESTRUCTORS
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 2009.
Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Friend Function. 2 Any data which is declared private inside a class is not accessible from outside the class. A non-member function cannot have an access.
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.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
C++ : Operator overloading example
Overloading C++ supports the concept of overloading Two main types
Operator Overloading Ritika Sharma.
Overloading Operator MySting Example
Visit for more Learning Resources
Operator Overloading.
Developed By : Ms. K. S. Kotecha
Polymorphism in C++ Operator Overloading
Objectives Define polymorphism Overload functions
Constructor & Destructor
Inheritance C++ strongly support the reusability, that is one class has been written and tested already it can be adapted or used to create a new class.
Operator Overloading BCA Sem III K.I.R.A.S.
Polymorphism Lec
Operator Overloading
Operators Lecture 10 Fri, Feb 8, 2008.
Operator Overloading.
Andy Wang Object Oriented Programming in C++ COP 3330
Operator Overloading; String and Array Objects
Chapter 6: UNDERSTANDING FRIENDS AND OVERLOADING OPERATORS
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.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Operator Overloading I
Chapter 6 Polymorphism.
Andy Wang Object Oriented Programming in C++ COP 3330
Presentation transcript:

Introduction Rules Types Programs OPERATOR OVERLOADING Introduction Rules Types Programs

Introduction Allows us to define the behavior of operators when applied to objects of a class Operator Overloading does not allow us to alter the meaning of operators when applied to built-in types Example: 2+3 addition, 2*3 multiplication By operator overloading, we can apply the operators to our own abstract data type like this Example; Class A{ int a,b; public: A(int x,int y){a=x;b=y;} }; main() { A obj1(2,3); -obj1; }

To Overload an Operator Steps Overloading an operator Write function definition as normal Function name : operator keyword followed by the symbol Operator+ () Applying operators on objects Create objects for the class Apply the operator on objects object1 + object2 -object1

Rules to Overload an operator Precedence of an operator cannot be changed Associativity of an operator cannot be changed Arity (number of operands) cannot be changed No new operators can be created ( Ex: +-) Use only existing operators No overloading operators for built-in types Cannot change how two integers are added Produces a syntax error (Ex; 2+3) Operator functions can be member or non- member functions

Rules to Overloade an operator C++ operators that can be overloaded C++ Operators that cannot be overloaded

Types Syntax Return type class name :: operator symbol(args){ } Using Member Function Unary Operator Overloading Ex: A operator –() ( ) Binary Operator Overloading Ex: A operator –(A object) ( ) Using Non Member Function (use Friend Keyword) Ex: A operator –(A object1, A object2) ( )

Unary operator : Member Function class A { int inches,feet; public: A(int x,int y) { inches=x; feet=y; } void display() { cout <<"\n"<<inches<<" "<<feet;} void operator-() { inches=-inches; feet=-feet; } }; int main() { A obj1(20,10); obj1.display(); -obj1; }

Binary operator Using Member Function class A { int a,b; public: A(int x,int y) { a=x; b=y; } void display() { cout <<"\n"<<inches<<" "<<feet;} void operator+(A o1); }; Void A :: operator +(A o1) a=a+o1.a; b=b+o1.b; }

int main() { A obj1(20,10); A. obj2(10,20); obj1+obj2; obj1 int main() { A obj1(20,10); A.obj2(10,20); obj1+obj2; obj1.display(); }

Unary operator : Non Member Function class A { int a,b public: A() { a=b=0; } A(int x,int y) { a=x; b =y; } void display() { cout <<"\n"<<a<<" "<<b;} friend A operator-(A) }; A operator-(A d1) A d2; d2.a=-d1.a; d2.b=-d1.b; return d2; } ;

int main() { A obj1(20,10); obj1.display(); A C; C=-obj1; C.display(); }

Unary operator : Non Member Function class A { int a,b public: A() { a=b=0; } A(int x,int y) { a=x; b =y; } void display() { cout <<"\n"<<a<<" "<<b;} friend A operator+(A,A) }; A operator+(A d1,A d2) A d3; d3.a=-d1.a+d2.a; d3.b=-d1.b+d2.b; return d3; } ;

int main() { A obj1(20,10); A obj2(20,10); obj1.display(); Obj2.display(); A C; C=obj1+obj2; C.display(); }