Virtual Function



Program to illustrate virtual function in C++





#include<iostream.h>
#include<conio.h>
class Base
{
public:
  virtual void display()
  {
    cout<<"\nIn Virtual Display of Base class";
  }
  void putdata()
  {
    cout<<"\nIn Putdata of base class";
  }
};
class Derived:public Base
{
public:
  void display()
  {
    cout<<"\nIn Display of Derived class";
  }
};
void main()
{
Base *ptr;

Base B;
ptr=&B;
clrscr();
ptr->display();

Derived D;
ptr=&D;
ptr->display();
ptr->putdata();
getch();
}

OUTPUT

In Virtual Display Of Base Class
In Display Of Derived Class
In Putdata Of Base Class


-----

Firoz Memon

Please view my other blogs:

          C++ Codes 4 Beginners

          Java Tips

          Java 4 Beginners

Previous Post Next Post