Advertisement
Doragonroudo

What is Virtual Function

Apr 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. // CPP program to illustrate
  2. // concept of Virtual Functions
  3. #include<iostream>
  4. using namespace std;
  5.  
  6. class base
  7. {
  8. public:
  9.     virtual void print ()
  10.     { cout<< "print base class" <<endl; }
  11.  
  12.     void show ()
  13.     { cout<< "show base class" <<endl; }
  14. };
  15.  
  16. class derived:public base
  17. {
  18. public:
  19.     void print ()
  20.     { cout<< "print derived class" <<endl; }
  21.  
  22.     void show ()
  23.     { cout<< "show derived class" <<endl; }
  24. };
  25.  
  26. int main()
  27. {
  28.     base *bptr;
  29.     derived d;
  30.     bptr = &d;
  31.      
  32.     //virtual function, binded at runtime
  33.     bptr->print();
  34.      
  35.     // Non-virtual function, binded at compile time
  36.     bptr->show();
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement