override in C++

 Override tells the compiler that this function is not only defined here but also in the parent class. 

  • This keyword is to indicate anyone that you are overriding the existing code. 
  • The override keyword will save from the bugs like return type mismatch, etc., So, it is a good practice to use it.

Overriding versions in the subclass don't need to specify again they are virtual. But you can, 

Use the keyword override: I'm overriding the function in base class.

Use the keyword final: No one should override me. 

When the method signature (name and parameters) are the same in the superclass and the child class, it's called Overriding.

If the function in super class(Army) doesn't have the qualifier virtual, but the function is overriden in derived class(ArmyHospital), then there is a possibility of segmentation fault during the dynamic binding. 


Comments