Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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

What is a node class in c++?

0 Answers  


What happens when you make call 'delete this;'?

0 Answers  


Does defining a function inline mean that it wont push and pop things on/off the stack ...like parameters and the return the address??

2 Answers  


Write a program for Divide a number with 2 and Print the output ( NOTE: Check for divide by zero error).

0 Answers  


What is iterator in c++?

0 Answers  


What is istream and ostream in c++?

0 Answers  


Explain differences between alloc() and free()?

0 Answers  


What does int * mean in c++?

0 Answers  


What are the static members and static member functions?

1 Answers  


What is the difference between a declaration and a definition?

0 Answers  


Do class declarations end with a semicolon?

0 Answers  


Where can I run c++ program?

0 Answers  


Categories