Skip to content

polymorphism

serhij.zolotukhin edited this page Feb 9, 2026 · 1 revision
  • What are virtual functions https://www.geeksforgeeks.org/cpp/cpp-polymorphism/ polymorphism - A single function name can work differently in different situations

    • Compile-Time Polymorphism (early binding / static polymorphism) This type of polymorphism is achieved by function overloading or operator overloading Function overloading is a feature of object-oriented programming where two or more functions can have the same name but behave differently for different parameters.
    • Runtime Polymorphism (late binding / dynamic polymorphism) Runtime polymorphism is implemented using function overriding with virtual functions

    https://www.geeksforgeeks.org/cpp/virtual-functions-and-runtime-polymorphism-in-cpp/ https://www.geeksforgeeks.org/cpp/virtual-function-cpp/ you cannot override a non-virtual method in C++ to achieve runtime polymorphism. Attempting to do so will result in function hiding, not overriding.

    • Overriding requires the base class function to be declared with the virtual keyword. This enables dynamic dispatch (runtime polymorphism), meaning the function call is resolved at runtime based on the actual object's type, not the type of the pointer or reference used to call it.
    • Hiding (or redefinition) occurs when you declare a function with the same name in the derived class as a non-virtual function in the base class. The derived class's function "hides" the base class's function. However, the function to be called is determined at compile time (static dispatch) based on the pointer/reference type.
  • What is the use of a virtual destructor? When deleting objects through a base class pointer, destructors must be virtual. This prevents memory leaks and ensures derived destructors are executed.

My side bar

Clone this wiki locally