polymorphism ppt
TRANSCRIPT
MEWAR INSTITUTE OF MANAGEMENT
SUBMITTED TO: ASHEESH PANDEY SIRSUBMITTED BY: SHEETAL SINGH
BCA 2ND YEAR 1590409046
POLYMORPHISM • The process of representing one form in multiple forms is known
as polymorphism• Polymorphism is derived from 2 Greek words: poly and morphs.
The word "poly" means many and morphs means forms. So polymorphism means many forms
Real life example
HOW CAN WE ACCESS THE MEMBER OF CLASS
The member of class can be access through pointer to the classGeneral syntax P->member class;P is pointer of object-> is member access operatorMember class it is the member of the object
POLYMORPHISM
COMPILE TIME
POLYMORPHISM
RUN TIME POLYMORPH
ISM
FUNCTION OVERLODI
NG
OPERATOR OVERLODI
NGVIRTUAL
FUNCTION
COMPILE TIME POLYMORPHISM
• It is also known as static binding, early binding and overloading as well
• It provides fast execution because known early at compile time compile time
• Polymorphism is less flexible as all things execute at compile time.
• It is achieved by function overloading and operator overloading
COMPILE TIME POLYMORPHISM
FUNCTION OVERLOADING
• Overloading refers to the use of the same thing for different purpose• Function overloading is a
logical method of calling several function with different datatype and argument but name of function is the same
OPERATOR OVERLOADING
• In C++ the overloading principle applies not only to functions, but to operators too.
• Overloaded operator is used to perform operation on user-defined data type. For example '+' operator can be overloaded to perform addition on various data types, like for integer, string(concatenation) etc.
RUN TIME POLYMORPHISM
• It is also known as dynamic binding, late binding and overriding as well
• It provides slow execution as compare to early binding because it is known at runtime
• Run time polymorphism is more flexible as all things execute at run time.
• It is achieved by virtual functions and pointers
VIRTUAL FUNCTION
It is a special type of function
It can be declared within base class and redefine by derived class
Its name will be same in every class
In derived class it will be executed through pointer of base class
It is declared by the keyword “virtual”
A redefined function is said to override the base class function
#include<iostream.h>class base{
public:void display()
{cout<<“display base”;}virtual void show()
{cout<<“\nshow base”;}};
class derived : public base
{public:
void display()
{cout<<“\n display derived”;}void show()
{cout<<“\n show derived”;}
};int main()
{base b;
derived d;base*bptr;
cout<<“\n bptr points to base”;
bptr=&b;bptr->display();
Bptr->show();Cout<<“\n bptr points to
derived”;Bptr=&d;
Bptr->display();Bptr->show();
Return 0;}
RULES OF VIRTUAL FUNCTION
• Virtual function must be member of some class• They can’t static member• Virtual function can be a friend of another class• We can’t have virtual constructor but we have virtual destructor• Virtual function is define in the base class it is not necessary to redefine in
the derived class
PURE VIRTUAL FUNCTION
• Pure virtual function give small definition to the abstract class . we need at least one pure virtual function to create abstract class• Pure virtual function must be define outside the function if it will
define inside the function then compiler show error