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?
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:
Overloading (C++ only) If you specify more than one definition for a function name or an operator in the same scope, you have overloaded that function name or operator. Overloaded functions and operators are described in Overloading functions (C++ only) and Overloading operators (C++ only), respectively.
An overloaded declaration is a declaration that had been declared with the same name as a previously declared declaration in the same scope, except that both declarations have different types.
If you call an overloaded function name or operator, the compiler determines the most appropriate definition to use by comparing the argument types you used to call the function or operator with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution, as described in Overload resolution (C++ only).
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
The difference between function overriding and virtual
function becomes important with polymorphism. Specifically when using references or pointers to a base class.
In C++, any derived class can be passed to a function requiring a base class object. (See also Slicing and LSP). Given:
struct Base_Virtual
{
virtual void some_virtual_function();
};
struct Base_Nonvirtual
{
void some_function();
};
void Function_A(Base_Virtual * p_virtual_base);
void Function_B(Base_Nonvirtual * p_non_virtual_base);
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.
Let us now test the polymorphism, especially virtual
vs. non-virtual (overriding methods).
The structures:
struct Derived_From_Virtual
: public Base_Virtual
{
void some_virtual_function(); // overrides Base_Virtual::some_virtual_function()
};
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
to Function_A
because Derived_From_Virtual
inherits from Base_Virtual
. I can also pass a pointer to Derived_From_Nonvirtual
to Function_B
.
virtual
and overridingThe virtual
modifier in Base_Virtual
, tells the compiler that Function_A
will use Derived_From_Virtual::some_virtual_function()
instead of the method in Base_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
to Function_B
, the compiler will instruct the function to use the method of the base class, Base_Nonvirtual::some_function()
. The some_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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With