C++ Logo.wine
C++

what is Virtual Functions? explain with example

Virtual Functions-

  • Virtual function is member function of a class.
  • Virtual function is declared with the keyword virtual.
  • Virtual function takes a different functionality in the derived class.
  • It is used to implement run time polymorphism.
For example:-
#include<iostream.h>
#include<conio.h>
Class pixeles
{
public:
virtual void disp()
{
cout<<“PIXELES CLASSES FOR BCA & MCA” ;
}
};
Class india : public pixeles
{
public:
void disp()
{
cout<<“\n A SUCCESS KEY”;
}
};
class success : public pixeles
{
public:
void disp()
{
cout<<“\n www.pixelesindia.com”;
}
};
void main()
{
clrscr();
pixeles op;
india pp;
success sll;
op.disp();
pp.disp();
ll.disp();
pixels *pt;
pt= &pp;
pt->disp();
pt=&sll
pt->disp();
getch();
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.