What is a virtual feature?

virtual features is a feature defined in SuperClass, which must be present in a subclass in order for this subclass to have a complete definition of the class. Virtual functions rely on an object -oriented programming paradigm called virtual inheritance, which is most often seen in C ++ using a "virtual" keyword. Two classes, superclass and subclass are required to define virtual functions. SuperClass is a place where the function is first declared and maybe defined. The subclass is where the function is defined - or overwritten, depending on whether the function has been defined in SuperClass.

virtual functions can be defined in one of two ways. First, it can be defined as a stump in which it has an empty body and does nothing. Secondly, it can be defined as a pure virtual feature where it is defined as a null in the SuperClass header.

There are advantages and disadvantages of both methodologies. Defining a function as a stump ensures that all subclass of haje it some implementation, even if it neaten. If one forgets to rewrite the function and properly implement it in a subclass, but it seems that it cannot point out any mistakes or warning. On the other hand, defining a pure virtual function requires each subclass to have its own definition of the function and the errors will appear if not.

, however, virtual functions are subject to the same hereditary rule as virtual features, so the hierarchy of more than two levels may not require explicit definitions of virtual functions. For example, class A, which declares a virtual function that is implemented in subclass B. Class B has its own subclass, class C C. C. C. Class C does not require an explicit definition of class A function as it will inherit the definition of class B, if necessary, class B can override

in other extreme, virtual functionsIt may not be defined in a subclass if virtual is declared in this subtlayer. For example, it is possible to consider class A, which declares a virtual function and has two subclass, B and C.

Class B to G must have a virtual class and somehow defined. If class B has implementation of function A, class D and E does not need to be reworked. Perhaps the subgances of C have to implement a function A, but both do something different, so defining the function in the class C itself would not be useful. In this case, the function may be declared virtual in class C and the implementation is not necessary.

virtual functions can be daunting to learn, but if they are used correctly, they can reduce code duplication and facilitate code understanding. However, there are many pitfalls with virtual functions, especially in terms of multiple heritage. In multiple heritage, it is possible that ambiguously defined virtual functions that would perceive each other should be used in this context with caution.

IN OTHER LANGUAGES

Was this article helpful? Thanks for the feedback Thanks for the feedback

How can we help? How can we help?