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.

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 9.
Sort the given string, without using string handling functions.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
Chapter 15: Operator Overloading
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.
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
Session Objectives Define Static data member Static member functions in a class Object as Function Argument Friend Function Friend Class.
Object Oriented Programming with C++/ Session 4/ 1 of 49 Operator Overloading Session 4.
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.
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.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
CS Object Oriented Programming Using C++
Operator Overloading. Introduction Computer is calculating machine. It calculates the data provided to it. It performs the various operations on data.
CONSTRUCTOR AND DESTRUCTORS
Operator overloading: Overloading a unary operator is similar to overloading a binary operator except that there is one Operand to deal with. When you.
Function Overloading and References
Chapter -6 Polymorphism
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.
 Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters.
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.
Chapter 13 Operator Overloading 1. Operator overload In C++, operator can be overload according to class type that you define. Operator overload is closely.
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 ++
Object-Oriented Programming (OOP) Lecture No. 16
Constructors and Destructors
Operator Overloading Ritika Sharma.
Friend functions.
Chapter 7: Expressions and Assignment Statements
Lecture 9: Operator Overloading
CONSTRUCTORS & DESTRUCTORS
Introduction Rules Types Programs
Operator Overloading.
CSC241: Object Oriented Programming
Chapter 7: Expressions and Assignment Statements
Constructor & Destructor
Object-Oriented Programming (OOP) Lecture No. 16
Operator Overloading BCA Sem III K.I.R.A.S.
CISC/CMPE320 - Prof. McLeod
Operator Overloading
Default Argument.
Operators Lecture 10 Fri, Feb 8, 2008.
Compile Time polymorphism.
Constructor Spl member fn auto ini of object
Operator overloading Dr. Bhargavi Goswami
COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Operator Overloading.
Object-Oriented Programming (OOP) Lecture No. 20
POLYMORPHISM ( in C++) POLYMORPHISM ( in C++). Presentation Outline Polymorphism Definition Types – Compile time and Run time polymorphism Function overloading.
INTERNATIONAL INSTITUTE OF IFORMATION TECHNOLOGY, (I²IT)
Chapter 6 Polymorphism.
Presentation transcript:

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 diff operations it is accomplished by a special fun called operator fun which describes the task Syntax data type operator opr(arg list) { function body; } opr  operator being overloaded The keyword operator is the function name

3 Operator Overloading Operator function can be either Member function OR Friend function Member function has no argument for unary operation Member function has only one argument for binary operation Friend function will have one argument for unary operation Friend function will have two arguments for binary operation

4 Operator Overloading Overloading unary operator using member fun Eg class s4 { int real,imaginary; public: s4(int x,int y){real=x;imaginary=y;} void show(){cout<<“\n”<<real<<“ + i”<<imaginary;} void operator –() // operator function def { real=real+10; imaginary=imaginary+10;} }; void main() { s4 C(1,2); C.show(); -C; // calling operator fun C.show(); }

5 Operator Overloading Overloading binary operator using member fun Eg class comp { int real,imaginary; public: comp(){}//default con comp(int a,int b){real=a; imaginary=b;} void show(){cout<<“\n”<<real<<“ + i”<<imaginary;} comp operator +(comp c) { comp t; t.real=real+c.real; t.imaginary=imaginary+c.imaginary; return t;} }; Void main() { comp c1(1,2), c2(2,5),c3; c1.show(); c2.show(); c3=c1+c2; c3.show(); }

6 Operator Overloading Overloading unary operator ++ using member fun class fib { int a,b,c; public: fib(){a=-1,b=1,c=a+b;} void display(){cout<<" "<<c;} void operator ++() {a=b; b=c; c=a+b;} }; void main() { fib F; for(int i=1;i<=5;i++) { F.display(); F++; }

7 Operator Overloading Overloading unary operator -using friend fun class s4 { int real,imaginary; public: s4(){} s4(int x,int y){real=x;imaginary=y;} void show(){cout<<"\n"<<real<<" + i"<<imaginary;} friend s4 operator -(s4); // friend fun decla }; s4 operator -(s4 s) // operator function def (friend) { s.real=s.real+10; s.imaginary=s.imaginary+10; return s;} void main() { s4 C(1,2); C.show(); s4 k=-C; // calling operator fun k.show(); }

8 Operator Overloading void main() { clrscr(); s4 A,B(2,7),C(1,2); B.show(); C.show(); A=B-C; A.show(); getch(); } Overloading binary operator -using friend fun class s4 { int real,imaginary; public: s4(){} s4(int x,int y){real=x;imaginary=y;} void show() {cout<<"\n"<<real<<" + i"<<imaginary;} friend s4 operator -(s4,s4); }; s4 operator-(s4 A,s4 B) { s4 t; t.real=A.real+B.real; t.imaginary=A.imaginary+B.imaginary; return t; }

9 Operator Overloading Overloading binary operator + using member fun class str { char *s; public: str(){} str(char *p) { s=new char[strlen(p)+1]; strcpy(s,p); } void show() { cout<<endl<<s; } str operator + (str k) { str t; t.s=new char[strlen(s)+strlen(k.s)]; strcpy(t.s,s); strcat(t.s,k.s); return(t); } }; void main() { clrscr(); str s1,s2="popo",s3="peepe"; s2.show(); s3.show(); getch(); }

10 Operator Overloading Overloading binary operator >using friend fun class str { char *s; public: str(char *l){s=new char[strlen(l)+1];strcpy(s,l);} void show(){cout<<s;} friend int operator >(str,str); }; int operator>(str s1,str s2) { if(strlen(s1.s)>strlen(s2.s)) {return 1;} else{return 0;} } void main() { str A("ajith"),B("sajith"); if(A>B) {cout<<"\n big is";A.show();} else {cout<<"\n big is ";B.show();} getch(); }

11 Operator Overloading Overloading binary operator ==using member fun class str { char *s; public: str(char *p) { s=new char[strlen(p)+1]; strcpy(s,p); } void show() { cout<<endl<<s; } int operator==(str k) { if(strcmp(s,k.s)==0) { return(1); } else { return(0); } } }; void main() { clrscr(); str s2="popo",s3="popo"; s2.show(); s3.show(); if(s2==s3) cout<<"\nequal"; else cout<<"\nnot"; getch(); }

12 Operator Overloading Overloading binary operator =using memb fun class comp {int real,img; public: comp(int a,int b){real=a;img=b;} void show() { cout<<real<<"+ i "<<img; } comp(){} void operator=(comp k) {real=k.real; img=k.img; } }; void main() {comp k1(10,20),k2; cout<<"first complex no "; k1.show(); k2=k1; cout<<"\nsec complex no "; k2.show(); }

13 Operator Overloading Overloading The function call operator () using memb fun class comp {int real,img; public: comp(int a,int b){real=a;img=b;} void show() { cout<<real<<"+ i "<<img; } comp(){} comp operator()(comp k) { comp p; p.real=real+k.real; p.img=img+k.img; return(p); } }; void main() { comp k1(10,20),k2(2,4),k3; cout<<"first complex no "; k1.show(); cout<<"\nsec comples no "; k2.show(); k3=k2(k1); cout<<"\nresult complex no "; k3.show(); }

14 Operator Overloading Overloading subscript operator [] using memb fun class comp {int real,img; public: comp(int a,int b){real=a;img=b;} void show() { cout<<real<<"+ i "<<img; } comp(){} comp operator[](comp k) { comp p; p.real=real+k.real; p.img=img+k.img; return(p); } }; void main() { comp k1(10,20),k2(2,4),k3; cout<<"first complex no "; k1.show(); cout<<"\nsec comples no "; k2.show(); k3=k2[k1]; cout<<"\nresult complex no "; k3.show(); }

1) Only existing operators can be overloaded 2) Cannot able to create new operators 3) cannot change the basic meaning of an operator 4) overloaded operator must have at least one operand Rules for operator overloading

5) we cannot be overloaded the following operators.- Membership operator ::- scope resolution operator ?:- conditional operator sizeof- size operator 6) overloaded operators follow the syntax rules of original operators Rules for operator overloading

7) Unary opr, overloaded by means of a member function, take no arg and does not return values 8) Unary opr, overloaded by means of friend fun, takes one arg Rules for operator overloading

9) Binary opr overloaded through a member fun, take one arg 10) binary opr overloaded through a friend fun take two arg 11) the binary opr such as +, -, *, / must return a value