OnlineCBT Logo OnlineCBT
C++ · LESSON 12

Polymorphism: Virtual Functions and Override Keyword

By Ratan Updated: 15 Oct 2026

Implement runtime dynamic polymorphism using virtual methods, vtables, and override keyword.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

Select behavior through a base interface

A virtual member function can dispatch to a derived implementation when called through a base pointer or reference. The override specifier asks the compiler to verify that the signature really overrides a virtual base member.

Complete example

#include 
#include 
#include 
class Notice {
public:
    virtual ~Notice() = default;
    virtual void show() const { std::cout << "General notice\n"; }
};
class ExamNotice : public Notice {
public:
    void show() const override { std::cout << "Exam starts at 10\n"; }
};
int main() {
    std::vector> notices;
    notices.push_back(std::make_unique());
    notices.push_back(std::make_unique());
    for (const auto& notice : notices) notice->show();
}

Expected lines: General notice and Exam starts at 10. The vector owns its notices through unique_ptr. The virtual destructor allows the correct derived destruction when a base pointer owns a derived object.

Static type and dynamic type

The expression may have a base-pointer static type while its target is a derived object. Virtual dispatch uses the object's dynamic type for an overridden function. Non-virtual members are selected by ordinary static lookup. A different parameter list is overloading or hiding, not an override.

Signature mistakes override catches

If the base declares show() const but the derived class writes show(), the const mismatch means it does not override the intended member. Adding override turns this mistake into a compiler diagnostic. You do not need to repeat virtual in every derived declaration; override communicates the intention.

Lifetime limitations

Virtual calls during construction or destruction do not dispatch into a more-derived part that has not yet been constructed or has already been destroyed. Avoid designs that depend on such calls reaching the final derived override. Also avoid storing derived objects by value in a vector of base objects: slicing removes the derived part.

Exercises

  1. Add a HolidayNotice whose show method prints a holiday message, then insert it into the same vector.
  2. Remove const from the derived signature while retaining override. Read the diagnostic and restore it.
  3. Why is the base destructor virtual? Ownership through a base pointer must destroy the complete derived object correctly.
  4. Replace the vector's unique_ptr elements with base values and explain the loss of derived behavior.

हिन्दी संकेत: base reference/pointer से virtual call करने पर actual object का override चलता है। Signature में const भी महत्वपूर्ण है।

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
पता संपर्क करें

+91 7877547686

ई-मेल

onlinecbtportal@gmail.com

हेल्पलाइन नंबर

+91 7877547686


डाउनलोड करने के लिए क्लिक करें
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat