Вы находитесь на странице: 1из 11

Virtual

Functions
And
Polymorphism

By
Bhagwant Singh

Contents
Virtual

Functions
Pure Virtual Functions
Virtual Constructor and Destructor.
Polymorphism
Runtime Or Dynamic Polymorphism

Virtual Function
In object-oriented programming, a virtual
function or virtual method is a function or
method whose behavior can be overridden
within an inheriting class by a function
with the same signature.
If the base class pointer has the address of
the derived class pointer will execute the
function in the base class.
The complier simply ignore the contents of
the pointer choose the member function
that matches the type of the pointer.

Example
Include<iostream>
Using namespace std
Class Bass
{
Public:
Void display(){cout<<\n Display
Base;}
Virtual Void show(){cout<<\n show
Base;}
};
Class Derived: public Base
{
Public:
Void display(){cout<<\n Display
Derived;}
Void show(){ \n show Derived
};

Int main()
{
Base B;
Derived D;
Base *bptr;
Cout<<\n bptr points to base \n;
bptr = &B;
bptr-> display();
bptr->show();
Cout<<\n bptr points to derived \n;
bptr = &D;
bptr-> display();
bptr->show();
Return 0;
}

Output
bptr points to base
Display Base
show Base
bptr points to derived
Display Base
show derived

Roles of Virtual Function


The virtual function should be the member of some class.
They cant be static member.
They are accessed using objects pointer.
A virtual function can be friend of another class
A virtual function in a base class must be defined even though it
may not be used.
The prototype of virtual function in base and derived should be
identical. Otherwise it will be consider as overloaded.
We cant have virtual constructor but we can have virtual
destructor.
While a base class pointer can point to any type of the derived
object the reverse is not true.
When a base pointer points to the derived class, increment and
decrement will not point to the next object of derived class
If the virtual function is defined in the base class, it need not be
necessarily redefined in the derived class. In such case call will
invoke base function.

Pure Virtual function


Virtual
0;
Keyword

void

return type

display() =

function

Declaration

Definition

Example
#include<iostream>
Using namespace std;
Class Bala
{
public:
virtual void example()=0;
};
Class C: public Bala
{
Public:
Void example()
{
Cout<<This is derived one
\n;
}
};

Class D: public Bala


{
Public:
Void example()
{
Cout<<This is derived two \n;
}
};
Void main()
{
Bala *bptr;
C c;
D d;
bptr-> &c;
Bprt->example();
bptr-> &d;
Bprt->example();
}

Virtual Constructor and


Destructor

Constructor cant be virtual

To create an object the constructor of the object


class must be the same type as the class.
At the time of calling the constructor the virtual
table would not have been created to resolve
any virtual function call. Hence it will have no
place to look up to

Virtual destructor are used when we want that


the different destructor in an inheritance
hierarchy are called in order.

Example
#include<iostream>
Using namespace std;
Class A
{
Public:
~A();
};
Class B
{
Public:
~B();
};

Void Main()
{
A *ptr = new B();
..
..
.
..
Delete ptr;
}

Output
Only

base class
destructor will be
called and it will
result in waste
memory.

If

we made base
class destructor as
virtual both the
destructor will be
called in the same
manner as that of
constructor
created

Вам также может понравиться