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

Answers were Sorted based on User's Feedback



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

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

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

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

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

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

Post New Answer

More C++ General Interview Questions

Is c++ still being used?

0 Answers  


How do you differentiate between overloading the prefix and postfix increments?

0 Answers  


Can we make any program in c++ without using any header file and what is the shortest program in c++.

0 Answers   MCN Solutions,


When does a 'this' pointer get created?

0 Answers  


What is c++ and its features?

0 Answers  






Explain stack unwinding.

0 Answers  


write asingle linked list which read from two list & the do the following 1 sort the prime & nonprime num (prime should be less tn nonprime) 2 each node has a prime num followd by nonprime 3 add a new node into its sutable plce 4 erase the most three duplicated non prime num 5 find the least duplicated prime num

0 Answers   Care,


What are the advantages of pointers?

0 Answers  


Shall we use 'free' to free memory assigned by new, What are the further consequences??

5 Answers   Symphony,


What is the role of C++ shorthand's?

0 Answers   TCS,


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

0 Answers  


What is enum c++?

0 Answers  


Categories