Submitted By-
Bhanu Pratap
BCA 3rdSem
Presentation on:-
 Basic of Polymorphism
 Run time Polymorphism
 Compile time Polymorphism
 Virtual Function
Polymorphism
• The word Polymorphism means having many forms.
• Polymorphism is a ability of a message to be displayed in more than
one form.
• Real life example of a Polymorphism, a person at a same time can
have different characteristic., Like a man at a same time is a
employee, a father or a husband. So a same person posses have
different behavior in different situation. This is called
Polymorphism.
In C++ Polymorphism is mainly divided into two types:-
• Compile time Polymorphism
• Run time Polymorphism
1-Compile time Polymorphism -
This type of Polymorphism is achieved by
function overloading or operator
overloading.
* Function Overloading - When there are multiple function with same name
but different parameters then these function
then these functions are said to be overloaded.
Functions can be overloaded by change in number of arguments or
type of arguments.
* Operator Overloading - If there are two objects of a class that
contains string as its data members. we
canredefine the meaning of + operator and use it to concatenate
those strings.
2- Run Time polymorphism:-
This type of polymorphism is achievd
by Function overriding.
And it is also know as late binding / Dynamic binding.
In late binding, the compiler identifies the type of object
run time and then matches the function call with the correct
function defination.
* Function overriding :-
If you create an object of the drived class
and call the member function which exists
in both classes (base and drived), the member funtion of the drived
class is invoked and the function of the basse class is ignored.
This feature of C++ is Known as Function overriding.
To access the overridden function of the base class from the
drived class, scope resolution operator :: is used.
* Virtual Function -
Virtual Function is a member Function of
base class which is overridden in the derived
class. The compiler perform Late Binding on this function.
To make a function virtual, we write the keyword Virtual before
the function definition.
Virtual Function is a part of Run Time Polymorphism.
Virtual Function
• The Virtual keyword can be used when declaring overriding
functions in a drived class.
• virtual Function declare in public which is declare within base
class.
• virtual function can be redefine or overwrite by drived class.
• object in virtual function is define in a drived class using a
pointer or a reference.
• virtual function mainly used to achieve Run Time Polymorphism.
class Base
{
public:
virtual void show()
{
cout << "Base class";
}
};
class Derived:public Base
{
{
cout << "Derived Class";
}
}
int main()
{
Base* b; //Base class pointer
Derived d; //Derived class object
b = &d;
b->show(); //Late Binding Ocuurs
polymorphism and virtual function

polymorphism and virtual function

  • 1.
  • 2.
    Presentation on:-  Basicof Polymorphism  Run time Polymorphism  Compile time Polymorphism  Virtual Function
  • 3.
    Polymorphism • The wordPolymorphism means having many forms. • Polymorphism is a ability of a message to be displayed in more than one form. • Real life example of a Polymorphism, a person at a same time can have different characteristic., Like a man at a same time is a employee, a father or a husband. So a same person posses have different behavior in different situation. This is called Polymorphism.
  • 4.
    In C++ Polymorphismis mainly divided into two types:- • Compile time Polymorphism • Run time Polymorphism 1-Compile time Polymorphism - This type of Polymorphism is achieved by function overloading or operator overloading. * Function Overloading - When there are multiple function with same name but different parameters then these function
  • 5.
    then these functionsare said to be overloaded. Functions can be overloaded by change in number of arguments or type of arguments. * Operator Overloading - If there are two objects of a class that contains string as its data members. we canredefine the meaning of + operator and use it to concatenate those strings.
  • 6.
    2- Run Timepolymorphism:- This type of polymorphism is achievd by Function overriding. And it is also know as late binding / Dynamic binding. In late binding, the compiler identifies the type of object run time and then matches the function call with the correct function defination.
  • 7.
    * Function overriding:- If you create an object of the drived class and call the member function which exists in both classes (base and drived), the member funtion of the drived class is invoked and the function of the basse class is ignored. This feature of C++ is Known as Function overriding. To access the overridden function of the base class from the drived class, scope resolution operator :: is used.
  • 8.
    * Virtual Function- Virtual Function is a member Function of base class which is overridden in the derived class. The compiler perform Late Binding on this function. To make a function virtual, we write the keyword Virtual before the function definition. Virtual Function is a part of Run Time Polymorphism.
  • 9.
    Virtual Function • TheVirtual keyword can be used when declaring overriding functions in a drived class. • virtual Function declare in public which is declare within base class. • virtual function can be redefine or overwrite by drived class. • object in virtual function is define in a drived class using a pointer or a reference. • virtual function mainly used to achieve Run Time Polymorphism.
  • 10.
    class Base { public: virtual voidshow() { cout << "Base class"; } }; class Derived:public Base {
  • 11.
    { cout << "DerivedClass"; } } int main() { Base* b; //Base class pointer Derived d; //Derived class object b = &d; b->show(); //Late Binding Ocuurs