Thursday, 21 September 2017

Virtual function demo in C++

/*
 Virtual function demo in C++
*/

#include "iostream"
using namespace std;

class Base{
 public: 
  virtual void show(){
   cout<<"\nIn Base";
  }
};

class Derive: public Base{
 public:
  void show(){
   cout<<"\nIn derive";
  }
};

int main(){
 Base *ptr, secondob;
 Derive ob;
 ob.show(); // in derive
 ptr = &ob;
 ptr->show(); // in derive
 secondob.show(); // in base


 
}
/*
 OUTPUT
[pavan@localhost VirtualFunction]$ g++ VFDemo.cpp
[pavan@localhost VirtualFunction]$ ./a.out 

In derive
In derive
In Base[pavan@localhost VirtualFunction]$ 

*/

No comments: