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?
The difference is just used when you call the method of the derived class, through a pointer to a base class object. In that moment you if the method you're calling was override in the derived class, you'll get the exceution of the base class, instead if was virtual then you got the execution of the derived class method.
For example: in this program
t->getA()
print"A in derived class"
, but if there'were no virtual modificator in the base class A, then it would print"A in base"
.Hope it helps.
This is more a follow up on the comments from this answer than an answer by itself.
virtual
is a keyword that requests runtime dispatch for the method being declared and at the same time declares the method as one of the overrides (implemented pure virtual methods aside). The method being declared, and any method that shares the exact signature and name in the deriving hierarchy from this class down are overrides. When you call a virtual method through a parent pointer or reference, the runtime will call the most derived override in the hierarchy of the called upon object.When a method is not virtual, and the same method is defined later on in the hierarchy, you are hiding the parent method. The difference here is that when the method is being called through a base pointer or reference it will be calling the base implementation, while if it is being called in a derived object it will call the derived implementation. This, among other cases, is called hiding because the base and derived functions are unrelated, and having it defined in the derived class will hide the base version from a call:
The difference, and what is wrong in the answer by @erik2red is that overrides are intimately related to virtual functions and imply that there is a runtime dispatch mechanism in place that determines the most derived override to call. The behavior that is shown in the answer and associated to override is actually the behavior when there are no overrides but rather method hiding.
Other issues
The language allows for pure virtual methods with implementation. It does not say anything about what terminology should be used with them, but a pure virtual method will never be considered for runtime dispatch. The reason is that when a classes with pure virtual methods (even if implemented) are considered abstract classes, and you cannot instantiate an object of the class. Once you have a derived class that provides an implementation for that method, that implementation becomes the final override in the hierarchy. The class can now be instantiated, but the pure virtual method will not be called through the runtime dispatch mechanism.
Virtual methods that are not the final override, as well as hided methods can be called if using the fully qualified name. In the case of virtual methods, using the fully qualified name disables the polymorphic dispatch mechanism for the call:
d.base::override()
will call the base implementation even if there are other overrides in deriving classes.A method can hide other methods in base classes even if the signatures do not match.
As with overrides,
d.base::f()
will call the base version, not because it disables polymorphism --it does not, as the method is not declared virtual it will never have polymorphic behavior-- but because the full qualification tells the compiler where the method is, even if it was hidden by another method in the derived class.When coming from Java, one might find the concept of virtual vs. non virtual member functions confusing. The thing to remember is that Java methods correspond to virtual member functions in C++.
The question is not so much why we actually have virtual functions, but why do we have non-virtual ones? The way I justify them to myself (correct me if I'm wrong) is that they are cheaper to implement, as calls to them can be resolved at compile time.
The classic example is that of a paint program where a base Shape class is created with a virtual draw() function. Then each of the shapes (circle, rectangle, triangle, etc) can be created as subclass that each implement their draw() function in the appropriate manner and the core paint program can keep a list of Shapes that will each do the appropriate draw() function even though only a pointer to the base Shape class is stored.