A virtual function in C++ is a member function in a base class that you mark withvirtual so that the correct overridden function in a derived class is chosen at runtime, even when you call it through a base-class pointer or reference.

Quick Scoop: Core Idea

  • You write virtual in the base class to enable runtime polymorphism (dynamic dispatch).
  • When you have a Base* or Base& pointing to a Derived object, calling the virtual function runs the Derived version, not the Base one.
  • Without virtual, C++ binds the function at compile time, so the base version is called even if the object is actually derived.

Simple Example (Story Style)

Imagine you have a base class Animal and different animals as derived classes:

cpp

class Animal {
public:
    virtual void speak() {  // virtual function
        std::cout << "Some animal sound\n";
    }
};

class Dog : public Animal {
public:
    void speak() override { // override the virtual function
        std::cout << "Woof\n";
    }
};

class Cat : public Animal {
public:
    void speak() override {
        std::cout << "Meow\n";
    }
};

int main() {
    Animal* a1 = new Dog();
    Animal* a2 = new Cat();

    a1->speak(); // Woof
    a2->speak(); // Meow

    delete a1;
    delete a2;
}
  • Both a1 and a2 are of type Animal*, but the actual object type (Dog or Cat) decides which speak() runs.
  • This is the “polymorphism” interviewers love to ask about.

Why Virtual Functions Exist

Virtual functions solve this problem:

“I have one common interface (base class pointer/reference), but I want different behavior depending on the real object type.”

Key benefits:

  • Enable runtime polymorphism (dynamic dispatch).
  • Let you write generic code that works with a base class but behaves correctly for each derived type (e.g., GUI widgets, game entities, drivers, etc.).
  • Are one of the core “ingredients” of object-oriented programming in C++ alongside inheritance and abstraction.

Important Rules and Facts

Some commonly-taught rules:

  1. You declare the function virtual in the base class.
  2. The function in the derived class must have the same prototype (same name, parameters, return type).
  3. You usually call virtual functions via base-class pointers/references to get polymorphic behavior.
  4. If a derived class does not override the function, the base implementation is used.
  1. Virtual functions cannot be static , but can be friend functions of another class.
  1. You can (and often should) have virtual destructors in polymorphic base classes, but constructors cannot be virtual.

Pure Virtual Function and Abstract Class

A pure virtual function is a virtual function with no implementation in the base class, declared like this:

cpp

class Shape {
public:
    virtual void draw() = 0; // pure virtual function
};
  • = 0 means: “Every derived class must implement this.”
  • A class with at least one pure virtual function becomes an abstract class ; you cannot create objects of it directly.

This is how you define an interface-like base class in C++.

Mini Forum-Style Take

“In C++, a virtual function is just a function in the base class that you expect to override, and you want calls to it to be resolved based on the real object type at runtime, not the pointer type.”

If you remember only one line for interviews, remember this.

Tiny “Latest / Trending” Note

Virtual functions themselves are an old core feature of C++, but they remain central in modern C++ (C++11–C++23) for building polymorphic class hierarchies, often combined with override, smart pointers, and abstract interfaces.

TL;DR

  • Definition: A virtual function is a base-class member function marked with virtual, meant to be overridden, and resolved at runtime based on the actual object type.
  • Use: Achieve runtime polymorphism when calling through base pointers/references.
  • Extras: = 0 → pure virtual → abstract class.

Information gathered from public forums or data available on the internet and portrayed here.