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

Download :PRESENTED BY: SHUBHAM KANDHARKAR SHUBHAM BHAGEL NIKET KOTHARI MANJEET SINGH ALHAWAT PRANAV TOSHNIWAL.

Post on 19-Dec-2015

213 views

Category:

Documents

1 download

TRANSCRIPT

  • Slide 1
  • :PRESENTED BY: SHUBHAM KANDHARKAR SHUBHAM BHAGEL NIKET KOTHARI MANJEET SINGH ALHAWAT PRANAV TOSHNIWAL
  • Slide 2
  • POLYMORPHISM COMPILE TIME POLYMORPHISM RUN TIME POLYMORPHISM FUNCTION OVERLOADING OPERATOR OVERLOADING VIRTUAL FUNCTIONS
  • Slide 3
  • 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.
  • Slide 4
  • // c + + program to demonstrate the concept of virtual function #include Class base { Public: Void display( ) { cout
  • base B; derived D; Base *bptr; Cout show( ); //calls base version Coutshow( ); //calls derived version return0;
  • Slide 7
  • Output : bptr points to base Display base Show base bptr points to derived Display base Show derived
  • Slide 8
  • 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
  • Slide 9
  • 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.
  • Slide 10
  • 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.
  • Slide 11
  • Remember that a class containing pure virtual functions cannot be used to declare any objects of its own.
  • Slide 12
  • //c ++ program to demonstrate the use of pure virtual function #include Class a { Public: virtual void example ( ) = 0; //denotes pure virtual function definition }; Class b : public a { Public:
  • Slide 13
  • Void example( ) { Cout
  • void main() { Exforsys* arra[2]; b e1; c e2; arra[0] = &e1; arra[1] = &e2; arra[0] -> example(); arra[1] -> example(); }
  • Slide 15
  • Output: DIEMS DIEMS AURANGABAD
  • Slide 16
  • THANK YOU