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

When is the copy constructor called?

730


What is a storage class?

748


What is a forward referencing and when should it be used?

671


I was a c++ code and was asked to find out the bug in that. The bug was that he declared an object locally in a function and tried to return the pointer to that object. Since the object is local to the function, it no more exists after returning from the function. The pointer, therefore, is invalid outside.

694


How should runtime errors be handled in c++?

714






Explain what are the sizes and ranges of the basic c++ data types?

740


What would happen on forgetting [], while deallocating an array through new?

718


What are the various arithmetic operators in c++?

667


What are the various situations where a copy constructor is invoked?

708


Differentiate between a copy constructor and an overloaded assignment operator.

730


Is c++ built on c?

660


What is a syntax in c++?

718


Can constructor be static in c++?

759


Which is the best c++ compiler?

679


What are the general quetions are in DEna bank manager IT/System interviews?

1647