Being requested to explain such a virtual function is appears to become probably the most common questions in interviews assessing fundamental C++ understanding. However, after many years programming C++, I have the uncomfortable feeling which i don't really know how better to define what they're.
Basically consult wikipedia, I see the phrase virtual function is:
"In object-oriented programming, an online function or virtual technique is a function or method whose behavior could be overridden inside an getting class with a function with similar signature"
This definition appears easy and elegant, and never C++ specific. But in my experience it does not appear to capture the idea of an online function in C++, since surely a non-virtual function may also be overridden inside an getting class with a function with similar signature.
If I am requested to explain such a virtual function is informally, I only say something about pointers like "it is a method so that whenever you refer to it as using a base class pointer, the version from it defined within the derived class is known as rather, when the pointer really indicates a clear case of a derived class". This does not appear just like a very elegant description from the concept. I understand that individuals say this is the way "polymorphism" is accomplished in C++ (polymorphism, so far as I realize, roughly being the entire concept of organizing objects into hierarchies), but I'm not sure of the more inticate method to understand or explain the mechanism than to undergo the example using the pointers.
I suppose I am unclear about if the "pointer" description of virtual functions is one thing fundamental for their definition, or simply something incidental for their implementation in C++.
I have always believed that this quote captures the essence of virtual functions:
An online function is really a method of determining a household of related actions that may be personalized through the organizations that actual need to do individuals actions.
Should you ignore all of the C++-isms of virtual functions - how getting virtual functions allows
dynamic_cast to get results for objects from the class type, how they are only treated virtually if utilized via a pointer, how virtual destructors are totally different from virtual non-destructors, etc. - I believe the above statement will get in the centre of the items virtual functions are only for.
The primary reason I love this statement is it describes virtual functions in ways that decouples them from programming. You can explain virtual functions to some non-technical person by using this definition by providing some concrete analogies. For instance, the thought of "activating an easyInch might be regarded as an online function since the actual mechanics of the items comes about when you switch on an easy is dependent positioned on the specific light you are using (incandescent? fluorescent? Brought?), however the conceptual idea is identical in each situation. Obviously, it isn't an ideal example, but It will get the purpose across good enough.
More generally, IMHO, if you are ever requested to explain something informally, try whenever possible to distance yourself in the particular programming language you are using and, if whatsoever possible, from computer systems whatsoever. Attempt to think about probably the most general establishing that the concept is applicable, after which describe it at this level. On the other hand, I train opening CS courses, therefore i have a little of the prejudice toward this domain, and so i have no clue how relevant this really is in the interview setting. :-)
since surely a non-virtual function may also be overridden inside an getting class with a function with similar signature.
No thats incorrect. The function only will get changed not overriddden for the reason that situation.
Your informal definition is a great review of such a virtual pointer does. It may sound like you are searching also to describe how it operates, consider the C++ standard does not specify, any "how" description could be specific to particular implementation and never the C++ language generally.
The C++ standard is about behavior and states next to nothing about implementation. You will find the "as-if" rule, which enables any alternate implementation providing you with exactly the same visible behavior.