What are virtual functions?

A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer. This allows algorithms in the base class to be replaced in the derived class, even if users don't know about the derived class

Showing Answers 1 - 9 of 9 Answers

Himanshu Mendiratta

  • Aug 30th, 2005
 

C++ virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes. 
C++ virtual function is, 
 
* A member function of a class 
* Declared with virtual keyword 
* Usually has a different functionality in the derived class 
* A function call is resolved at run-time 

Pravesh Kumar

  • Aug 30th, 2005
 

C++ virtual functions are used to acheive run time polymorphism. To declare a virtual function , virtual keyword is put at the start of normal function declaration.  
Requirements for implementing virtual function- 
[/LIST] 
* Base Class Pointer 
* Inheritance 
* Method Overriding. 

  Was this answer useful?  Yes

bhuvana

  • Oct 4th, 2005
 

 

 A virtual function allows derived classes to replace the implementation provided by the base class.

  Was this answer useful?  Yes

It is a functions whose behaviour can be overriden with an inherited class by a function of same signature. It is an important part of OOPS and Polymorphism.

  Was this answer useful?  Yes

MCBod

  • Mar 31st, 2010
 

A virtual function is a member function of a class fro which it expects child functions to provide their own implementations. My specifiying the function as virtual we allow polymorphism to take place, in that child classes will have their own version of the function invoked, regardless of whether or not the class is accessed as an instance of the parent class or as an instance of the child class.
A pure virtual function is a function without implementation, thus rendering the class abstract and requiring all class instances to actually be an instances of some child class

  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