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
Why is the function main() special?
What is the difference between ++ count and count ++?
What is a modifier in c++?
Is it possible to use a new for the reallocation of pointers ?
Show the declaration for a static function pointer.
How would you use the functions sin(), pow(), sqrt()?
How can you quickly find the number of elements stored in a static array? Why is it difficult to store linked list in an array?
What is an accessor in c++?
How do you master coding?
What is the best way to take screenshots of a window with c++ in windows?
Is swift faster than go?
Can we sort map in c++?
List down the guideline that should be followed while using friend function.
Why is c++ not purely object oriented?
How would you differentiate between a pre and post increment operators while overloading?