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
Can we define a constructor as virtual in c++?
What is the use of typedef?
What new()is different from malloc()?
What are the different types of comments allowed in c++?
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?
Write about c++ storage classes?
How does c++ sort work?
Differences between private, protected and public and give examples.
Write a program to find the Fibonacci series recursively.
Where the memory to the static variables is allocated?
What are libraries in c++?
What can I safely assume about the initial values of variables which are not explicitly initialized?
What is the best way to declare and define global variables?
Define virtual constructor.
Can you please explain the difference between using macro and inline functions?