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

Showing Answers 1 - 15 of 15 Answers

sandesh

  • Dec 7th, 2005
 

use switch statment !!!

  Was this answer useful?  Yes

Yashwant Pinge

  • Apr 27th, 2006
 

Virtual function:The virtual function is a member function of class which overrides the functionality in a derived classThe virtual function concept is not in a c which is in C++

  Was this answer useful?  Yes

praveen

  • Jan 10th, 2007
 

   
we can implement virtual functions concept  by using function pointers.
if any one have more on this explain about this

  Was this answer useful?  Yes

dasam

  • Apr 12th, 2007
 

C++ virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes.

Virtual fundtions can be implemented using the keyword 'virtual' in the funtion declaration.

The property of virtual functions in C can be achieved by using function pointers or pointers to function.

  Was this answer useful?  Yes

satish_sudi

  • Jun 25th, 2007
 

#include <stdio.h>


/* Though this code doesn't reflect exact C++ polymorphism(dynamic),
 i.e. same interface but different inplementation. Tried with different name
*/
void base(int dummy){
  printf("Recieving dummy from base - %d n", dummy);
}

void derive(int dummy){
  printf("Recieving dummy from der - %d n", dummy);
}

int main() {

 int dummy = 100;
 void (*funptr)(int);
 char ch;

 scanf("%c", &ch);
 
 /* Switch case to store the two interfaces address according to the input */
 switch (ch){
 case 'b':
  funptr=&base;
  break;
 case 'd':
  funptr=&derive;
  break;
 }

 /* Function pointer, which, is now, pointing to the function either "base" or "der". */
 
 funptr(dummy);

    return 0;
}

stman

  • Jul 11th, 2007
 

In C you would use an array of pointer-to-functions and call the functions by using the array index.

  Was this answer useful?  Yes

ramyajaya

  • Jun 2nd, 2011
 

virtual functions are used to avoid overriding, when a function in the base class is said as virtual then the same function name when being used in the derived class, even then the function in derived class will be executed, else if the function is not set as virtual the base class function only be executed

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions