What is a virtual method in C ++?

What is a virtual method in C ++?

+4
source share
3 answers

A virtual function allows derived classes to replace the implementation provided by the base class. The compiler ensures that the replacement is always called whenever the object in question is actually in a derived class, even if the object is accessed by a base pointer, not a derived pointer. This allows you to replace the algorithms in the base class in the derived class, even if users are not aware of the derived class.

A derived class can either completely replace ("override") a member function of a base class, or a derived class can partially replace ("complement") a member function of a base class. The latter is true if the member function of the derived class calls, if necessary, the member function of the base class.

More details here:

http://www.codersource.net/c/c-tutorials/c-virtual-function.aspx

However, it is very simple. Try Google next time before coming here to ask!

+6
source

Virtual functions were created , so you do not need to switch between types.

+1
source

This is a method that is viewed at runtime instead of compilation time. This allows you to work with types such as polymorphism.

0
source

All Articles