MEWAR INSTITUTE OF MANAGEMENT
SUBMITTED TO: ASHEESH PANDEY SIR
SUBMITTED BY: SHEETAL SINGH
BCA 2ND YEAR
1590409046
POLYMORPHISM
&
ITS TYPE
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
HOWCAN WE ACCESS THE MEMBER OF CLASS
The member of class can be access through pointer to the
class
General syntax
P->member class;
P is pointer of object
-> is member access operator
Member class it is the member of the object
POLYMORPHISM
COMPILE TIME
POLYMORPHIS
M
RUN TIME
POLYMORPHIS
M
FUNCTION
OVERLODIN
G
OPERATOR
OVERLODIN
G
VIRTUAL
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
PROGRAM RELATED
TO
VIRTUAL 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;
}
OUTPUT
bptr points to base
display base
show case
bptr points to derived
display base
show derived
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
polymorphism ppt

polymorphism ppt

  • 1.
    MEWAR INSTITUTE OFMANAGEMENT SUBMITTED TO: ASHEESH PANDEY SIR SUBMITTED BY: SHEETAL SINGH BCA 2ND YEAR 1590409046
  • 2.
  • 3.
    POLYMORPHISM • The processof 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
  • 4.
    HOWCAN WE ACCESSTHE MEMBER OF CLASS The member of class can be access through pointer to the class General syntax P->member class; P is pointer of object -> is member access operator Member class it is the member of the object
  • 5.
  • 6.
    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
  • 7.
    COMPILE TIME POLYMORPHISM FUNCTIONOVERLOADING • 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.
  • 8.
    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
  • 9.
    VIRTUAL FUNCTION It isa 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
  • 10.
  • 11.
    #include<iostream.h> class base{ public: void display() {cout<<“displaybase”;} 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; }
  • 12.
    OUTPUT bptr points tobase display base show case bptr points to derived display base show derived
  • 13.
    RULES OF VIRTUALFUNCTION • 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
  • 14.
    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