Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.

Slides:



Advertisements
Similar presentations
Operator Overloading Fundamentals
Advertisements

Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
2 Objectives You should be able to describe: Operator Functions Two Useful Alternatives – operator() and operator[] Data Type Conversions Class Inheritance.
Chapter 13: Overloading.
Chapter 15: Operator Overloading
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Operator overloading Object Oriented Programming.
Operator Overloading in C++
1 CSC241: Object Oriented Programming Lecture No 07.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Operator Overloading and Type Conversions
Chapter 12: Adding Functionality to Your Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
1 Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
Learners Support Publications edited by Taranjit singh Aulakh, BGIET sangrur,CSE deptt Constructors and Destructors.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
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.
©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.
CONSTRUCTOR AND DESTRUCTORS
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Operator overloading: Overloading a unary operator is similar to overloading a binary operator except that there is one Operand to deal with. When you.
Chapter -6 Polymorphism
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
 Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters.
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.
CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
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.
Learners Support Publications Constructors and Destructors.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Operator Overloading.
Object-Oriented Programming (OOP) Lecture No. 16
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Operator Overloading Ritika Sharma.
Function Overloading Can enables several function Of same name
Chapter 13: Overloading and Templates
Submission Example May 14, 2018
Constructor & Destructor
Object-Oriented Programming (OOP) Lecture No. 16
Operator Overloading BCA Sem III K.I.R.A.S.
Operator Overloading
Operator Overloading; String and Array Objects
Overview of C++ Overloading
Constructors and Destructors
Operator overloading Dr. Bhargavi Goswami
Pointers Dr. Bhargavi Goswami Department of Computer Science
Operator Overloading.
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)
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Chapter 6 Polymorphism.
Presentation transcript:

Unit VI polymorphism

Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism (static binding)  Run time polymorphism (dynamic binding) The compile time polymorphism (early binding) is achieved through function and operator overloading. The dynamic (late) binding is achieved through virtual functions.

Md.Jaffar Sadiqsumalathasumalatha Function overloading Overloading refers to the use of the same thing for different purposes. Function overloading: Same function name is used to perform different tasks. (same function name with different signatures) In c++, the functions may have same name with different argument list, then the functions are said to be overloaded. The parameter list is different in terms of:  The types of the parameters  The number of arguments  The order of the arguments in the list

Md.Jaffar Sadiqsumalathasumalatha Ex: An overloaded abc() function handles different data types of data: function prototypes : 1. void abc(); 2.void abc(int); 3.void abc(double); 4.void abc(int, int); 5.int abc(int, float); The function calls : abc(); abc(10,20.0); abc(10.0); abc(30); abc( 10, 10); Calls 1. prototype Calls 5. prototype Calls 3. prototype Calls 2. prototype Calls 4. prototype

Md.Jaffar Sadiqsumalathasumalatha A function call first matches the prototype having same number and type of arguments and then calls the appropriate function for execution. The correct match must be unique. Ex: void sum( int,int); Int sum(int, int); In this case the sum() is not said to be overloaded, because the signatures of these functions are same. And when we call the function sum(10,10), Compiler will generate an error.

Md.Jaffar Sadiqsumalathasumalatha Friend functions A friend function is a non member function to any class,even though it has capability to access the private data of any class. In order to make an outside function is friendly to a class, we need to use a keyword called friend before a function declaration. Ex: Class ABC { ……… public: friend function _name (arguments); }; Return_ type function _name( arguments) { …………} Main() { ……… function_name(arg); } Friend function declaration Friend function definition Friend function call

Md.Jaffar Sadiqsumalathasumalatha Characteristics of friend function Friend functions are non member functions. So it can not access the private members directly and has to use an object name and dot operator with each member. Class sample { int x ; public : sample() { x=0 ;} friend void update( sample); void show(); }; void update( sample s) { s.x++; } Void sample :: show() { cout << x ; } Main() { sample s1; update(s1); s1.show(); }

Md.Jaffar Sadiqsumalathasumalatha Member functions of one class can be friend functions of another class. Class A { public: int fun1(); }; Class B { public: friend int A :: fun1(); }; An non member function may access the data from more than one class with the help of friend Class ABC { …………. friend void show( ABC,XYZ); …….. }; Class XYZ { ……….. friend void show (ABC,XYZ); …….. };

Md.Jaffar Sadiqsumalatha #include Using namespace std; class XYZ; // forward declaration class ABC { int a; public: ABC() {a=9;} friend void max(ABC,XYZ); }; class XYZ { int b; public: XYZ() { b=10} friend void max(ABC,XYZ); }; Void max( ABC A, XYZ X) { if (A.a > X.b) cout<< A. a<< “is greater” ; else cout<<X.b<<“is greater”; } Main() { ABC A; XYZ X ; max(A,X); }

Md.Jaffar Sadiqsumalatha #include class complex { float x,y; public: complex() { } complex(float r,float i) { x=r; y=i; } void sum (complex,complex ); }; Void complex:: sum(complex c1, complex c2) { complex c ; c.x= c1.x+c2.x; c.y=c1.y+c2.y; cout<<"The sum is:" <<c.x<<"+i"<<c.y; } main() { complex c1(10,10); complex c2(20,20); complex c3; c3.sum(c1,c2); } Addition of two Complex numbers

Md.Jaffar Sadiqsumalatha #include class complex { float x,y; public: complex() { } complex(float r,float i) { x=r; y=i; } complex (complex &,complex &); }; complex:: complex(complex &c1, complex &c2) { complex c ; c.x= c1.x+c2.x; c.y=c1.y+c2.y; cout<<"The sum is:" <<c.x<<"+i"<<c.y; } main() { complex c1(10,10); complex c2(20,20); complex c3(c1,c2); } Addition of two complex numbers using copy constructor

Md.Jaffar Sadiqsumalatha #include class complex { float x,y; public: complex() { } complex(float r,float i) { x=r; y=i; } friend complex sum (complex,complex ); Void display(); }; Complex sum(complex c1, complex c2) { complex c ; c.x= a.x+b.x; c.y=a.x+b.y; return c; } void complex ::display() { cout <<x<<“+”<<y; } main() { complex c1(10,10); complex c2(20,20); complex c3; c3=sum(c1,c2); c3.display(); } Addition of two complex numbers using friend function

Md.Jaffar Sadiqsumalatha Operator overloading C++ permits us to provide an additional meaning to existing operators. Using operator overloading, we can add two user defined objects with the same syntax that is applied to the basic types Ex: a+b ( a, b are integers) (a+b) ( a, b are two user defined data types ),it is possible using operator overloading) –Examples of already overloaded operators Operator << is both the stream-insertion operator and the bitwise left-shift operator

Md.Jaffar Sadiqsumalatha Overloadable operators All operators can be overloaded except the following operators operator category operators member access dot operator scope resolution :: conditional ?: pointer to member * size of data type sizeof()

Md.Jaffar Sadiqsumalatha Rules for overloading operators Overloading restrictions –Precedence of an operator cannot be changed –Associativity of an operator cannot be changed –Arity (number of operands) cannot be changed Unary operators remain unary, and binary operators remain binary Operators &, *, + and - each have unary and binary versions Unary and binary versions can be overloaded separately No new operators can be created –Use only existing operators No overloading operators for built-in types

Md.Jaffar Sadiqsumalatha Defining operator overloading The operator function is used to define the additional task to an operator. The general form of an operator function is: Return type class name::operator op (arg) {………. } where op is the operator being overloaded. Ex: operator+ used to overload the addition operator (+). The operator function must be either member function or friend function sumalatha

Unary Operator Overloading -Unary operator takes only one operand. -Unary operators are unary -, ++, - -, etc., For example a minus operator takes one operand (-m) We know that this operator changes the sign of an operand when applied to a basic data item. Example Program void main() { int m=100; m=-m; cout<<m; }

Md.Jaffar Sadiqsumalatha Overloading unary minus operators sumalatha Class sample { int x, y ; Public: sample(int a, int b) { x=a;y=b; } void operator-(); Void display(); }; Void sample::operator-() { x=-x; y=-y; } Void display() { cout<<x<<y; } Main() { sample S(3,4); S.display(); -S; // calls operator-() function s.display(); }

Md.Jaffar Sadiqsumalatha Overloading unary minus operator using friend function Class sample { int x, y ; Public: sample(int a, int b) { x=a;y=b; } friend void operator-(sample &); Void display(); }; Void operator-(sample & s) { s.x=-s.x; s.y=-s.y; } Void display() { cout<<x<<y; } Main() { sample S(3,4); S.display(); operator–(S); // calls operator-() function s.display(); }

Binary Operator Overloading -Binary operator takes two operands. -Binary operators are +, -, *, etc., For example a plus operator takes two operands (a + b) Example Program void main() { int a=100,b=200,c; c=a+b; cout<<c; }

Md.Jaffar Sadiqsumalatha #include class complex { float x,y; public: complex() { } complex(float r,float i) { x=r; y=i; } complex operator+ (complex ); void display(); }; complex complex:: operator+ ( complex c2) { complex c ; c.x= x+c2.x; c.y=y+c2.y; return c; } Void complex :: display() { cout<<x<<“+”<<y;} main() { complex c1(10,10); complex c2(20,20); complex c3; c3=c1+c2 // c3.operaor+(c1,c2); c3.display(); } Overloading binary operators

Md.Jaffar Sadiqsumalatha #include class complex { float x,y; public: complex() { } complex(float r,float i) { x=r; y=i; } friend complex operator+ (complex,complex); void display(); }; complex complex:: operator+ (complex c1, complex c2) { complex c ; c.x= c1.x+c2.x; c.y=c1.y+c2.y; return c; } Void complex :: display() { cout<<x<<“+”<<y;} main() { complex c1(10,10); complex c2(20,20); complex c3; c3.operaor+(c1,c2); c3.display(); } Overloading binary operators using friend function

Md.Jaffar Sadiqsumalatha Operator function can be declared as member function or using friend function. We cannot use friend functions to overload certain operators. However, member functions can be used to overload them operator category operators Assignment operator= Function call operator() Subscripting operator[] Class member access operator -> There are certain situations where we will use only friend function rather than member function. Consider a statement A= 2 + B where A,B are two objects of same class 2 is integer type. Here we can’t use member function because the left hand operand must be an object. A =2.operator+(B) is illegal But this can be achieved through friend function i.e : A=operator+ ( 2, B)