I am new to C++.
Could anybody tell me the difference between method overriding and virtual function concepts in c++.
The functionality of virtual functions can be over-ridden in its derived classes. Redefining a function in a derived class is called function overriding.
why do we actually have virtual functions?
ABSTRACT
In this paper, we discuss virtual functions in C++. Part zero explains how virtual functions are declared and overridden. Part one attempts (and perhaps fails) to explain how virtual functions are implemented. Part two is a sample program that uses the example classes defined in parts zero and one. Part three is the classic animal example given in every virtual function - polymorphism tutorial.
PART ZERO
A method of a class is said to be virtual if and only if its declared to be so.
(Of course, I am assuming the programmer did not previously do anything like
#define virtual
.)A class that redeclares and re-implements a non-virtual method of one of its bases is said to overload that method. A class that redeclares and re-implements a virtual method of one of its bases is said to override that method.
PART ONE
When the compiler detects a class has virtual methods, it automatically adds a virtual method table (also known as vtable) to the class' memory layout. The result is similar to what would have been generated from compiling this code:
When the compiler detects a class has overridden a virtual method, it replaces its associated entry in the vtable. The result is similar to what would have been generated from compiling this code:
PART TWO
Now that it's clear that virtual functions are implemented using vtables, which are nothing but a bunch of function pointers, it should be clear what this code does:
PART THREE
Since no virtual function example is complete without an example with animals...
Virtual function / method is simply a function whose behavior can be overriden within a subclass (or in C++ terms a derived class) by redefining how the function works (using the same signature).
Think of a base class mammal with a speak function. The function is void and simply couts how a mammal speaks. When you inherit from this class you can override the speak method so that dogs go "Arf Arf!" and cats go "Meow Meow".
Your question seems to ask what are the differences, well there are none because with virtual functions one can override the behavior of these functions. You may be after the difference between overriding functions and overloading them.
Overloading functions means to create a function with the same name but different arguments i.e. different number- and type-of argument(s). Here is an explanation on overloading in C++ from IBM's site:
As for the full rational reason for situations where virtual functions are required, this blog post gives a good one: http://nrecursions.blogspot.in/2015/06/so-why-do-we-need-virtual-functions.html
Helicopters and airplanes both fly, but they do it in different ways -- they are both instances of some hypothetical object Flyer . You can ask the Flyer object to "fly" -- but Flyer is just a interface It doesn't know anything about flying other than that it should be able to fly.
However if both the helicopter and airplane follow the interface of flyer, than if had an airfield object and you gave it a Flyer all the airfield needs to do is to request the flyers to fly.
For example:
C++ is a strict type-safe language, and this sort of functionality (making function calls to derived classes indirectly via a base class) is only possible when RTTI is enabled for the object hierarchy, and qualifying a member function virtual enables this.
Virtual functions exist to help design the behavior of a base class. A base class of pure virtual functions cannot be instantiated and is called an abstract class.
It's up to the derived classes to implement those methods described by the virtual functions in the base class. Derived classes can then be instantiated (they exist and occupy memory).
Deriving from derived classes can redfine a function already defined in the parent object. This technique you already know as overriding and allows you to customize the behavior of this child object.
As you learn more C++, you'll find that inheritance isn't all that it's cracked up to be. Composition and is often a better alternative. Have fun.
The difference between function overriding and
virtual
function becomes important with polymorphism. Specifically when using references or pointers to a base class.The Base Setup
In C++, any derived class can be passed to a function requiring a base class object. (See also Slicing and LSP). Given:
In the above code, there are two base classes, one declares a virtual method, the other declares a non-virtual function.
Two functions are declared that require pointers to the respective base clases.
The Derived Classes
Let us now test the polymorphism, especially
virtual
vs. non-virtual (overriding methods). The structures:struct Derived_From_Nonvirtual : public Base_Nonvirtual { void some_function(); }
According to the C++ language, I can pass a pointer to a
Derived_From_Virtual
toFunction_A
becauseDerived_From_Virtual
inherits fromBase_Virtual
. I can also pass a pointer toDerived_From_Nonvirtual
toFunction_B
.The Difference Between
virtual
and overridingThe
virtual
modifier inBase_Virtual
, tells the compiler thatFunction_A
will useDerived_From_Virtual::some_virtual_function()
instead of the method inBase_Virtual
. This is because the method is virtual, the final definition may reside in a future or derived class. The actual definition says to use the method in the most derived class containing the definition.When passing a pointer to
Derived_From_Nonvirtual
toFunction_B
, the compiler will instruct the function to use the method of the base class,Base_Nonvirtual::some_function()
. Thesome_function()
method in the derived class is a separate, unrelated, method from the base class.The primary difference between
virtual
and overriding occurs with polymorphism.Check out C++ FAQ lite, http://www.parashift.com/c++-faq-lite/. is probably one of the best C++ resources for beginners. it has in-depth write-up about virtual functions and overriding.
I personally found C++ FAQ to be an excellent source as I learn C++. Other people have different opinion, your mileage may vary