VIRTUAL FUNCTIONS
:PRESENTED BY:
SHUBHAM KANDHARKAR
SHUBHAM BHAGEL
NIKET KOTHARI
MANJEET SINGH ALHAWAT
PRANAV TOSHNIWAL
POLYMORPHISM
COMPILE TIME POLYMORPHISM
RUN TIME POLYMORPHISM
FUNCTION OVERLOADING
OPERATOR OVERLOADING
VIRTUAL FUNCTIONS
• When we use the same function name in both base and derived classes, the function in base class is declared as virtual using the keyword virtual preceding its normal declaration. • When a function is made virtual, c ++ determines which function to use at run time based on the type of objects pointed to by the base pointer, rather than the type of the pointer. Thus making the base pointer to point to different objects, we can execute different versions of the virtual function.
// c + + program to demonstrate the concept of virtual function#include<iostream.h>Class base{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( ){Cout<<“\n Show derived” ;}};int main( ){
base B;derived D;Base *bptr;
Cout<<“\n bptr points to base \n” ;bptr=&B;bptr -> display( ); //calls base versionbptr -> show( ); //calls base version Cout<<“\n\n bptr points to derived \n” ;bptr = &D;bptr ->display( ) ; //calls base versionbptr ->show( ); //calls derived version return0;
Output :bptr points to base
Display base Show base
bptr points to derived
Display base Show derived
Advantages and dissadvantages Advantages
1. We can override functionality in (Derived or Child) class.
Disadvantages
1. The function call takes slightly longer due to the virtual mechanism , and it also makes it more difficult for the compiler to optimize because it doesn't know exactly which function is going to be called at compile time
2. Using base class object we can access, derived class functions.
2. In a complex system, virtual functions can make it a little more difficult to figure out where a function is being called from. Or, to figure out why a function isn't being called if someone overrode it with a new virtual function.
• When we have not defined any object of class media and therefore the function show( ) in the base class has been defined empty .such functions are called as do nothing functions.• A ‘do-nothing’ function may be defined as follows:virtual void show( ) = 0;• such do-nothing functions are called as virtual functions.• A pure virtual function is a function declared in a base class that has no definitions relative to the base class.
• Remember that a class containing pure virtual functions cannot be used to declare any objects of its own.
//c ++ program to demonstrate the use of pure virtual function#include<iostream.h>Class a{Public:virtual void example ( ) = 0;//denotes pure virtual function definition };Class b : public a{Public:
Void example( ){Cout<<“DIEMS”<<endl;}
Class c : public a{Public:
void example ( ){cout<<“DIEMS AURANGABAD”<<endl;}
};
void main(){Exforsys* arra[2];b e1;c e2;arra[0] = &e1;arra[1] = &e2;
arra[0] -> example();arra[1] -> example();}
Output:DIEMSDIEMS AURANGABAD
THANK YOU