What are Virtual Functions? How to implement virtual
functions in "C" ?

Answer Posted / arati pradhan

A function declared with the keyword virtual, termed as
virtual function.It is used for runtime polymorphism. When
a function is declared as virtual function, the function of
that object is invoked which is refernced by the base
pointer.

Example-
class BASE
{
public:
virtual void show()
{
cout<<"withen base";
}
};
class DERIVED:public BASE
{
public:
void show()
{
cout<<"withen derived";
}
};
void main()
{
BASE *b,b1;
DERIVED d1;
b=&b1;
b->show(); //call the BASE show()
b=&d1;
b->show(); //call the DERIVED show()
getch();
}

Is This Answer Correct ?    9 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can we define a constructor as virtual in c++?

852


What is the use of typedef?

857


What new()is different from malloc()?

853


What are the different types of comments allowed in c++?

803


How can you quickly find the number of elements stored in a dynamic array? Why is it difficult to store linked list in an array?

807


Write about c++ storage classes?

988


How does c++ sort work?

778


Differences between private, protected and public and give examples.

797


Write a program to find the Fibonacci series recursively.

861


Where the memory to the static variables is allocated?

809


What are libraries in c++?

825


What can I safely assume about the initial values of variables which are not explicitly initialized?

851


What is the best way to declare and define global variables?

968


Define virtual constructor.

834


Can you please explain the difference between using macro and inline functions?

773