Content deleted Content added
Show in upper case or pipe? |
|||
Line 4:
In [[object-oriented programming]] such as is often used in [[C++]] and [[Object Pascal]], a '''virtual function''' or '''virtual method''' is an inheritable and [[Method overriding (programming)|overridable]] [[function (computer science)|function]] or [[method (computer science)|method]] that is [[dynamic dispatch|dispatched dynamically]]. Virtual functions are an important part of (runtime) [[Polymorphism (computer science)|polymorphism]] in [[object-oriented programming]] (OOP). They allow for the execution of target functions that were not precisely identified at compile time.
Most programming languages, such as [[JavaScript]], [[PHP]] and [[Python (programming language)|Python]], treat all methods as virtual by default<ref>{{Cite web|title=Polymorphism (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)|url=https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html|access-date=2020-07-11|website=docs.oracle.com}}</ref><ref>{{Cite web|title=9. Classes — Python 3.
== Purpose ==
{{
The concept of the virtual function solves the following problem:
Line 87:
*/
static void _Llama_eat(struct Animal *self) {
printf("<Llama at %p> Llamas eat grass!\n", ( void* )(self));
}
Line 113:
Although pure virtual methods typically have no implementation in the class that declares them, pure virtual methods in some languages (e.g. C++ and Python) are permitted to contain an implementation in their declaring class, providing fallback or default behaviour that a derived class can delegate to, if appropriate.<ref>[https://en.cppreference.com/w/cpp/language/destructor#Pure_virtual_destructors Pure virtual destructors - cppreference.com]</ref><ref>[https://docs.python.org/3/library/abc.html "abc — Abstract Base Classes: @abc.abstractmethod"]</ref>
Pure virtual functions can also be used where the method declarations are being used to define an [[interface (Java)|interface]]
== Behavior during construction and destruction ==
Line 134:
* [[Virtual class]]
* [[Interface (object oriented programming)]]
* [[Component Object Model|Component object model]] (Microsoft's COM)
* [[Virtual method table]]
|