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

What is runtime errors c++?

821


Evaulate: 22%5 a) 2 b) 4 c) 0

872


Why is c++ is better than c?

717


Describe the role of the c++ in the tradeoff of safety vs. Usability?

962


Can the creation of operator** is allowed to perform the to-the-power-of operations?

774


Write a Program for dynamically intialize a 2 dimentional array. Eg:5x20, accept strings and check for vowels and display the no.finally free the space allocated .

931


Describe public access specifiers?

813


Is it possible to use a new for the reallocation of pointers ?

814


Can you declare an array without a size in c++?

790


Differentiate between a constructor and a method in C++.

782


If you hear the cpu fan is running and the monitor power is still on, but you did not see anything show up in the monitor screen. What would you do to find out what is going wrong?

817


What is the use of endl in c++?

794


What are enumerations?

864


What is the use of 'this' pointer?

1026


How java is different from c and c++?

935