answersLogoWhite

0

Virtual functions (or virtual methods as they are also known), are methods declared in a class that are expected to be overridden by a derived class. That is, the method is a generic method that applies to the class in which it is declared (the base class), but that may require modification by a more-specialised class (the derived class). When you implicitly call the base class method, the overridden method is called instead, thus ensure the correct behaviour. The overridden can still call the base class method explicitly, as can any other code that has access to the base class method.

If you do not declare generic methods to be virtual, overriding those methods in the derived class may have unexpected side-effects, particularly if the method is overloaded in the base class. For instance, a derived class' override will effectively hide all the overloads of the base class.

Note that if you declare any virtual function, then you are essentially notifying the end-user that the class is intended to act as a generic class and may be derived from, if required. However, if the function is declared pure-virtual, you are notifying the end-user that the class is abstract and mustbe derived from (it is not optional).

Also, if there are any virtual methods then the destructor must be virtual as well, to ensure the derived object is destroyed before the base class is destroyed.

It is often good practice to declare all methods as virtual if there's at least one virtual method, however this only applies to methods that would require alternative handling by more-specialised classes. If the base class can provide a complete implementation that requires no further specialisation, it needn't be declared virtual.

Although there is a memory cost involved in declaring virtual methods, the bulk of that cost is paid with the first virtual function which establishes the virtual table (v-table). Thereafter, the cost is minimal. The v-table ensures the most-derived method is called, thus ensuring correct behaviour of derived classes, even when working with the generic base class.

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

Is it possible to declare a keyword as an identifier in c plus plus?

No. Keywords are reserved and cannot be used as identifiers. However, C/C++ is case-sensitive. So although register is a reserved keyword, Register is not.


What is meant by println in c plus plus?

println is not a C++ keyword.


How java use extern key word which is used in C plus plus?

No extern keyword in Java.


What keyword is used to find derivative in C plus plus?

The lazy way is to use a dynamic cast. The correct way is to use virtual functions in the base class. That way you don't ever need to know the derived type -- it simply does what is expected of it.


What is the Difference between override and new keyword in C?

An override is the specialisation of a virtual function. The new keyword instantiates an instance of an object in dynamic memory and returns a reference to that object (or null if the object could be instantiated). Both are used in C++, but not C.


Is in is used as a keyword in C language?

Neither "in" nor "is" is a keyword in C.


What is use of keyword extra in c plus plus programming?

Nothing.


How does the use of the 'void' keyword differ in C plus plus vs. C?

It doesn't. Void has the same meaning in both.


Is there any keyword in c or c plus plus like the function inkey in qbasic?

Using TurboC? kbhit and getch are your friends


Instantiation of objects in c plus plus?

Objects are instantiated when statically declared or dynamically created with the new keyword.


Why you use zero instead of NULL in c plus plus?

In C++ NULL is defined as 0. It's a design failure, will be fixed with a new 'nullptr' keyword.


What is friendly in c plus plus?

The keyword "friend" allows a function or variable to have access to a protected member inside a class.