Thursday, 27 October 2016

Virtual Functions

#include <iostream>
using namespace std;
 class Animals{
   public :
     virtual void speak(){
      cout<<"Animals speak random";
    }
};
 
class Dog: public Animals{
  public:
      void speak(){
      cout<<"Dogs woof";
    }
};
 
int main()
{
  Animals *a;
  Dog d;
 
  a = &d;
 
  a->speak();
}
 
Output:
 //without virtual speak()
 Animals speak random
 //with virtual speak()
 Dog woof

No comments:

Post a Comment