chapter -6 polymorphism concept of polymorphism :- polymorphism means many forms. it is the ability...

13
Polymorphism Concept of polymorphism :- Polymorphism means many forms. It is the ability to take more than one form. In object oriented programming, polymorphism refers to identically named method i.e. member functions that have a different behavior depending on the type of object they refers. For example, an operation may exhibit different behavior in different instances. The behavior depends upon the types of data used in the operation. Following figure illustrates that a single function name can be used to handle different numbers and different types of arguments.

Upload: barbara-daniels

Post on 19-Jan-2016

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter -6 Polymorphism Concept of polymorphism :- Polymorphism means many forms. It is the ability to take more than one form. In object oriented programming,

Chapter -6Polymorphism

Concept of polymorphism :-Polymorphism means many forms. It is the

ability to take more than one form. In object oriented programming, polymorphism refers to identically named method i.e. member functions that have a different behavior depending on the type of object they refers. 

For example, an operation may exhibit different behavior in different instances. The behavior depends upon the types of data used in the operation.  

Following figure illustrates that a single function name can be used to handle different numbers and different types of arguments.

Page 2: Chapter -6 Polymorphism Concept of polymorphism :- Polymorphism means many forms. It is the ability to take more than one form. In object oriented programming,

•There are basically two types of polymorphism.

1.Compile time polymorphism2.Run time polymorphism.

•Compile time polymorphism: The overloaded member functions are selected for invoking by matching arguments, both type and number. This information is known to the compiler at the compile time and therefore, compiler is able to select the appropriate function for a particular call. This is called early binding or static linking. Also known as compile time polymorphism, early binding, simply means that object is bound to its function call at compile time.

Page 3: Chapter -6 Polymorphism Concept of polymorphism :- Polymorphism means many forms. It is the ability to take more than one form. In object oriented programming,

Run time polymorphism:

The appropriate member function is selected while the program is running; this is known as runtime function. C++ supports mechanism known as virtual function, to achieve run time polymorphism. At run time, when it is known what class objects are under consideration, the appropriate version of the function is called. Since, function is linked with a particular class much later after the compilation; this process is termed as late binding. It is also known as dynamic binding because the selection of the appropriate function is done dynamically at runtime. Dynamic binding requires the use of pointers to objects.

Page 4: Chapter -6 Polymorphism Concept of polymorphism :- Polymorphism means many forms. It is the ability to take more than one form. In object oriented programming,

•Function overloading :- function overloading allows a function to operate differently depending on the type and no. of arguments passed to it. When a function is overloaded, the same function has more than one definition where the difference is either in no. of parameters or in data types of parameters or both. Therefore depending on the way the function is called that corresponding function definition is used to call the function Overloading is a feature of C++ that allows a function to behave in more than one way also called as compile time polymorphism.

The data types and/or number of arguments with which the function is called, determines which function definition will be used to call the function.

Page 5: Chapter -6 Polymorphism Concept of polymorphism :- Polymorphism means many forms. It is the ability to take more than one form. In object oriented programming,

Example :

int add(int a, int b); // function add with two integer parameters

double add(double p, double q); // function add with two double parameters

float add(float a, float b, float c); // function add with three float parameters

Therefore when function is called as add(1, 2, 3) then the third definition will be used whereas if function is called as add(12.3, 12.4) the second definition will be used and if it is called as add(10, 20) then the first definition will be used.

In function overloading, a number of functions are defined with the same names. The execution of particular function depends upon the it’s data type, no. of arguments and sequence of arguments declared .

Example :

int add(int,int); int add(int,int,int);double add(double,double);

Now, if function add() is invoked, the parameter to it will determine which version of the function will be executed and this resolution is done at compile time.

Page 6: Chapter -6 Polymorphism Concept of polymorphism :- Polymorphism means many forms. It is the ability to take more than one form. In object oriented programming,

Example :-

#include<iostream.h>#include<conio.h>int add(int,int);int add(int,int,int);double add(double,double);main(){clrscr();cout << "\n The addition of two number is " << add(10,20);cout << "\n The addition of three number is " << add(10,20,30);cout << "\n The addition of two double number is " << add(10.5,20.5);getch();return 0;}int add(int a, int b){ return(a+b); }int add(int a, int b,int c) { return(a+b+c); }double add(double a, double b) { return(a+b); }

Page 7: Chapter -6 Polymorphism Concept of polymorphism :- Polymorphism means many forms. It is the ability to take more than one form. In object oriented programming,

•Function overriding :-

Overriding means base class member can be overridden by defining a derived class member with same name as that of the base class member.The function which is overridden works differently in the base class and the derived class. Complier makes a decision in following way to execute member function, if same member function exists in base class & derived class.•If the function is invoked from an object of the derived class, then the function in derived class is executed.•If the function is invoked from an object of the base class, then the base class member function is invoked.

The overridden function should have same name in base class as well as in derived class but it can have different no. of parameters of different data types and also different return types.

Page 8: Chapter -6 Polymorphism Concept of polymorphism :- Polymorphism means many forms. It is the ability to take more than one form. In object oriented programming,

// program to illustrate member function overriding

class baseclass // base class definition{ public:void disp(void) // base class function to be

overridden{cout<<"\n Printing from Base class";}};class derivedclass // derived class definition.{public:void disp(void) // overriding base class function in

derived class.{cout<<"\n Printing from Derived class";}};

Page 9: Chapter -6 Polymorphism Concept of polymorphism :- Polymorphism means many forms. It is the ability to take more than one form. In object oriented programming,

void main(void){clrscr( );baseclass x; // creating object of base

classderivedclass y; // creating object of

derived classx.disp( ); // base class function

invokedy.disp( ); // derived class function

invoked}

Output of Program :

Printing from Base classPrinting from derived class

Page 10: Chapter -6 Polymorphism Concept of polymorphism :- Polymorphism means many forms. It is the ability to take more than one form. In object oriented programming,

•Virtual function:

A function qualified by virtual keyword, when a virtual function is called via a pointer, the class of the object pointed to by the pointer determines which function definition will be used. It allows functions in base class to be declared in each derived class. A pointer to an object of a base class can also point to the objects of its derived classes.The rule is that the compiler selects the function based on the contents of the pointer, not on the type of pointer Virtual function should be declared in the public section of the class.

Syntax for declaration of virtual function virtual return_data_type function_name(<parameter list>){ <body of function>}

Page 11: Chapter -6 Polymorphism Concept of polymorphism :- Polymorphism means many forms. It is the ability to take more than one form. In object oriented programming,

Example :-#include<iostream.h>#include<conio.h>class base{public: // base class definition.virtual void print( void ) // virtual function in

base class{cout<<" \n Printing from base class";}

};class derived : public base // defining first

derived class{public:

void print(void) // overriding virtual function.

{cout<<" \n Printing from first derived class

";}

};

Page 12: Chapter -6 Polymorphism Concept of polymorphism :- Polymorphism means many forms. It is the ability to take more than one form. In object oriented programming,

Rules for virtual functions :

• The virtual function must be members of some class.• They cannot be static members.• The are accessed by using object pointer• The virtual function can be a friend of another class.• A virtual function in a base class must be defined, even though it may not be used.• We cannot have virtual constructor but can have virtual destructors.• While a base pointer can point to any type of the derived object, the reverse is not true. That is, we can not use a pointer to a derived class to access an object of the base type.• If a virtual function is defined in the base class, it need not be necessarily redefined in the derived class.

Page 13: Chapter -6 Polymorphism Concept of polymorphism :- Polymorphism means many forms. It is the ability to take more than one form. In object oriented programming,

•Differentiate between run-time polymorphism and compile time polymorphism.

Or•Differentiate between early and late binding.Or•Differentiate between static and dynamic binding.