Virtual Functions | Polymorphism | OOP

shubhamarsenal 2,180 views 5 slides Sep 26, 2016
Slide 1
Slide 1 of 5
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5

About This Presentation

Introduction & importance of virtual functions in object oriented programming.


Slide Content

VIRTUAL FUNCTIONS Firoj Ghimire Shubham Ghimire Suyash Nepal

What is Virtual function ? A virtual function is a member function that is declared within a base class and redefined by a derived class. To create virtual function, precede the function’s declaration in the base class with the keyword virtual. When a class containing virtual function is inherited, the derived class redefines the virtual function to suit its own needs . A virtual function uses a single pointer to base class pointer to refer to derive all the objects. Virtual functions belongs to the branch of Runtime Polymorphism in C++

Syntax

How virtual function works ? Base class pointer can point to derived class object. In this case, using base class pointer if we call some function which is in both classes, then base class function is invoked. But if we want to invoke derived class function using base class pointer, it can be achieved by defining the function as virtual in base class, this is how virtual functions support runtime polymorphism.

Pure Virtual Function Virtual function without function body is pure virtual function. Syntax virtual int area()=0 //pure virtual function
Tags