Posts

Showing posts from May, 2024

C++ Virtual Functions uses and Limitations

Image
 What is a Virtual Function in C++? C++ Virtual function is a Class member function used with Inheritance in  C++ to achieve Runtime Polymorphism . We know Polymorphism means functions with the same name but different arguments. In  Runtime Polymorphism Parent and Child classes have functions with the same name and also the same arguments which is called Overriding . Base class functions are prepended with the keyword virtual so that they can be overridden in child classes to add more functionality.   The difference between virtual and non-virtual is  Call to non-virtual functions resolved at compile time based on the type of Object pointer/Reference on which it is called. Call to virtual functions resolved at runtime based on the actual object to which the pointer/reference is pointing. 1 Runtime Polymorphism Before going to Virtual Function,  let's discuss more about the Runtime Polymorphism with some example Runtime Polymorphism exists because in C+...