answersLogoWhite

0

Importance of virtual functions

Updated: 8/11/2023
User Avatar

Wiki User

11y ago

Best Answer

Private virtual functions are useful when you expect a particular method to be overridden, but do not wish the override to be called from outside of the base class. That is, the base class implementation and its overrides remain private to the base class.


Private virtual methods are particularly useful in implementing template method patterns, where certain algorithmic steps need to be deferred to subclasses, but where those steps need not be exposed to those subclasses. In many cases the private virtual methods will be declared pure-virtual, thus rendering the base class an abstract base class.


User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

Different computer language has different meaning of virtual functions.

In C#, the advantages of declare a method being virtual are:

  1. It is the default implementation. The derived classes do not need to write one if it is a desired behavior.
  2. If the function/method only provides partial implementation, the derived class may choose to override it with additional codes, while the default implementation is still available to be called by the override method.
  3. Or, the derived class may override the complete method as its specialized way to do the named operation.
This answer is:
User Avatar

User Avatar

Wiki User

12y ago

A virtual function (or method) is only required in classes from which you expect to derive another class and you expect that particular method to be overridden in the derived class. However, there is no actual requirement to override a virtual method in a derived class because the base class must provide a default implementation for that method. If the default implementation is sufficient for all derived classes, then there is no need to declare the method virtual.

Classes with virtual methods must contain a v-table (virtual method table), so there is some memory overhead involved, as well as a performance penalty with the additional level of indirection within the derived classes. If your class is small and you do not expect to derive any classes from it, its usually best not to declare any virtual methods.

After you declare a method as virtual, the bulk of the overhead is already paid -- each additional virtual method will consume a little more memory, but the table itself already exists and the extra level of indirection is unavoidable. The upshot is that your code is much easier to maintain because the need for duplicate code is greatly reduced.

Note that if you declare any virtual method, you should also declare the destructor as virtual. You should also examine all the non-virtual methods and be certain you understand why they are non-virtual; the assumption is that if any one method is virtual, then all methods should be virtual.

Derived classes that inherit virtual methods do not need to declare those methods as virtual (it is assumed), but it gives a visual reminder which functions you've overridden and which are exclusive to the derived class.

Note that when a base class method is declared non-virtual and it has one or more overloads, and a derived class overrides the method name, then all the overloads within the base class become hidden from the derived class. To ensure all the overloads are visible to the derived class, declare them as virtual.

Virtual methods can also be declared pure-virtual. In this case, the base class becomes abstract -- meaning you cannot create an instance of the class. You may provide a default implementation of the method, but it is not required because derived classes must provide the implementation for a pure-virtual method (even if only to call the base class method).

Derived-classes that do not fully-implement the pure-virtual methods of their base class (or classes) become abstract themselves. Only fully-implemented derived classes of abstract classes can actually be instantiated.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Virtual functions are a part of polymorphic programming. A virtual function is defined in a class, and may be overridden in a child class that inherits from the parent class. This is important because it allows developers to create a class that models the behavior of multiple types of objects that have a common ancestor with a default implementation, and override "one-off" scenarios where the default algorithm doesn't work. This is different than the abstract keyword, where each child class must implement the function, and behaves differently than a normal function, which cannot be overridden by the child class (the parent implementation would always be called).

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Importance of virtual functions
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you represent a C plus plus non-pure virtual function in UML?

All virtual functions (including pure-virtual functions) are represented in italics. All non-virtual functions are represented normally. There is no differentiation between pure and non-pure virtual functions, however some people append "=0" to distinguish the pure-virtual functions.


What is virtual function table?

A virtual function table is a table of pointers to functions.


Which binding virtual function used?

Virtual functions are dynamically bound at runtime.


What are the Functions and importance of preamble?

what is the function and importance of preamble


What types of functions cannot be made virtual?

Static member functions, member function templates and constructors cannot be virtual.


Do you use virtual functions in Java?

Every method in java that isn't a class (static) method is automatically "virtual." If you want to disable virtual overrides, make the method "final" in the base class.


What are the importance of circular functions in everyday life?

importance of circular function


Which function are used in dyanamic programming handling?

Virtual functions.


How does virtual function support run time polymorphism?

Virtual functions are used to suport runtime polymorphism.In C++,if we have inheritance and we have overridden functions in the inherited classes,we can declare a base class pointer and make it to point to the objects of derived classes.When we give a keyword virtual to the base class functions,the compiler will no do static binding,so during runtime ,the base class pointer can be used to call the functions of the derived classes.Thus virtual functions support dynamic polymorphism.


What functions are completely unsupported in DOS?

DOS had no support for virtual memory, no native GUI, and no built in security functions.


How is run time polymorphism accomplished?

Through inheritance and virtual functions.


What is the importance of using functions in a c program?

C programs do not function without functions.