What are Virtual Functions? How to implement virtual
functions in "C" ?
Answers were Sorted based on User's Feedback
Answer / 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 |
Answer / jaroosh
To address the second part of the question - the only way to
implement mechanism like virtual functions in C is by using
function pointers, creating structure emulating Virtual
Pointer Table etc.
Implementation is rather complicated and creates a lot of
overhead for every structure that we want to use with
virtual functions (since ANSI C doesnt support inheritance
and polimorphism, it also calls for emulating this behavior,
so virtual functions for C is actually more of a form of art
than anything usefull).
Is This Answer Correct ? | 7 Yes | 2 No |
Answer / rama
When we use the same function name in both the base and the
derived classes, the function in base class is declared as virtual...we must access virtual function through the use of a pointer declared as a pointer to the base class..
In other words, virtual function is defined in the base class,it need not be necessarily redefined in the derived class. In such cases,the respective calls will invoke the base class function.
Is This Answer Correct ? | 2 Yes | 0 No |
How const functions will be treated by compiler?
How do you declare A pointer to function which receives an int pointer and returns a float pointer
Is c++ a programming language?
Difference between overloaded functions and overridden functions
How can you say that a template is better than a base class?
Explain selection sorting?
What is ios :: in in c++?
Is c# written in c++?
Write a Program for read a line from file from location N1 to N2 using command line arguments. Eg:exe 10 20 a.c
plz send me National informatics center paper pattern
What is the Standard Template Library?
What are C++ inline functions?