Virtual function: Difference between revisions

Content deleted Content added
Removed section by SyedHC: redundant explanation of what's already explained in the "Abstract classes" section, but poorly written and incomplete, and placed ahead of more fundamental concepts, and using self-published blog as a source.
Tags: Mobile edit Mobile web edit Advanced mobile edit
Line 10:
The concept of the virtual function solves the following problem:
 
In [[object-oriented programming]], when a derived class inherits from a base class, an object of the derived class may be referred to via a [[Pointer (computer programming)|pointer]] or [[Reference (computer science)|reference]] of the base class type instead of the derived class type. If there are base class methods overridden by the derived class, the method actually called by such a reference or pointer can be bound (linked) either '"early'" (by the compiler), according to the declared type of the pointer or reference, or '"late'" (i.e., by the runtime system of the language), according to the actual type of the object is referred to.
 
Virtual functions are resolved '"late'". If the function in question is '"virtual'" in the base class, the most-derived class's implementation of the function is called according to the actual type of the object referred to, regardless of the declared type of the pointer or reference. If it is not '"virtual'", the method is resolved '"early'" and selected according to the declared type of the pointer or reference.
 
Virtual functions allow a program to call methods that don't necessarily even exist at the moment the code is compiled.{{Citation needed|date=October 2021}}