Transcript
Page 1: :PRESENTED BY: SHUBHAM KANDHARKAR SHUBHAM BHAGEL NIKET KOTHARI MANJEET SINGH ALHAWAT PRANAV TOSHNIWAL

VIRTUAL FUNCTIONS

:PRESENTED BY:

SHUBHAM KANDHARKAR

SHUBHAM BHAGEL

NIKET KOTHARI

MANJEET SINGH ALHAWAT

PRANAV TOSHNIWAL

Page 2: :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

Page 3: :PRESENTED BY: SHUBHAM KANDHARKAR SHUBHAM BHAGEL NIKET KOTHARI MANJEET SINGH ALHAWAT PRANAV TOSHNIWAL

• 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.

Page 4: :PRESENTED BY: SHUBHAM KANDHARKAR SHUBHAM BHAGEL NIKET KOTHARI MANJEET SINGH ALHAWAT PRANAV TOSHNIWAL

// 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” ;}};

Page 5: :PRESENTED BY: SHUBHAM KANDHARKAR SHUBHAM BHAGEL NIKET KOTHARI MANJEET SINGH ALHAWAT PRANAV TOSHNIWAL

Class derived : public base {Public:Void display( ){Cout<<“\n Display derived” ;}Void show( ){Cout<<“\n Show derived” ;}};int main( ){

Page 6: :PRESENTED BY: SHUBHAM KANDHARKAR SHUBHAM BHAGEL NIKET KOTHARI MANJEET SINGH ALHAWAT PRANAV TOSHNIWAL

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;

Page 7: :PRESENTED BY: SHUBHAM KANDHARKAR SHUBHAM BHAGEL NIKET KOTHARI MANJEET SINGH ALHAWAT PRANAV TOSHNIWAL

Output :bptr points to base

Display base Show base

bptr points to derived

Display base Show derived

Page 8: :PRESENTED BY: SHUBHAM KANDHARKAR SHUBHAM BHAGEL NIKET KOTHARI MANJEET SINGH ALHAWAT PRANAV TOSHNIWAL

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

Page 9: :PRESENTED BY: SHUBHAM KANDHARKAR SHUBHAM BHAGEL NIKET KOTHARI MANJEET SINGH ALHAWAT PRANAV TOSHNIWAL

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.

Page 10: :PRESENTED BY: SHUBHAM KANDHARKAR SHUBHAM BHAGEL NIKET KOTHARI MANJEET SINGH ALHAWAT PRANAV TOSHNIWAL

• 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.

Page 11: :PRESENTED BY: SHUBHAM KANDHARKAR SHUBHAM BHAGEL NIKET KOTHARI MANJEET SINGH ALHAWAT PRANAV TOSHNIWAL

• Remember that a class containing pure virtual functions cannot be used to declare any objects of its own.

Page 12: :PRESENTED BY: SHUBHAM KANDHARKAR SHUBHAM BHAGEL NIKET KOTHARI MANJEET SINGH ALHAWAT PRANAV TOSHNIWAL

//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:

Page 13: :PRESENTED BY: SHUBHAM KANDHARKAR SHUBHAM BHAGEL NIKET KOTHARI MANJEET SINGH ALHAWAT PRANAV TOSHNIWAL

Void example( ){Cout<<“DIEMS”<<endl;}

Class c : public a{Public:

void example ( ){cout<<“DIEMS AURANGABAD”<<endl;}

};

Page 14: :PRESENTED BY: SHUBHAM KANDHARKAR SHUBHAM BHAGEL NIKET KOTHARI MANJEET SINGH ALHAWAT PRANAV TOSHNIWAL

void main(){Exforsys* arra[2];b e1;c e2;arra[0] = &e1;arra[1] = &e2;

arra[0] -> example();arra[1] -> example();}

Page 15: :PRESENTED BY: SHUBHAM KANDHARKAR SHUBHAM BHAGEL NIKET KOTHARI MANJEET SINGH ALHAWAT PRANAV TOSHNIWAL

Output:DIEMSDIEMS AURANGABAD

Page 16: :PRESENTED BY: SHUBHAM KANDHARKAR SHUBHAM BHAGEL NIKET KOTHARI MANJEET SINGH ALHAWAT PRANAV TOSHNIWAL

THANK YOU


Top Related