Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 71

Virtual Function Syntax in C++

In this lesson: Quick reference for virtual function syntax in C++ with override and final — short examples. In Hindi & English.

Quick recap

A virtual function lets a call through a base pointer run the derived version at run time. This page is a compact syntax reference; for how it works internally (the vtable), virtual destructors, and the full concept, see the main virtual functions lesson and polymorphism.

Basic syntax

C++
class Base {
public:
    virtual void draw();      // 'virtual' in the base
};
class Derived : public Base {
public:
    void draw() override;     // 'override' in the derived
};

Base-pointer example

C++
#include <iostream>
using namespace std;
class Animal { public: virtual void speak() { cout << "...\n"; } };
class Dog : public Animal { public: void speak() override { cout << "Woof\n"; } };
class Cat : public Animal { public: void speak() override { cout << "Meow\n"; } };

int main() {
    Animal *arr[2] = { new Dog(), new Cat() };
    for (Animal *a : arr) a->speak();   // run-time dispatch
}
Output:
Woof
Meow

With vs without virtual

With virtualWithout virtual
basePtr->f() runsDerived's versionBase's version
DispatchRun time (dynamic)Compile time (static)

Pure virtual syntax

Add = 0 to make a function pure virtual — no body, and derived classes must implement it. This makes the base an abstract class:

C++
class Shape {
public:
    virtual double area() = 0;   // pure virtual
};

See abstract classes for the full story.

Keyword cheat sheet

KeywordWhereMeaning
virtualBase functionCan be overridden; enables dynamic dispatch
overrideDerived functionVerifies it overrides a base virtual
= 0Base functionPure virtual; class becomes abstract
finalFunction/classPrevents further overriding/derivation

Common mistakes

  • Writing = 0 but still giving the function a body without meaning to.
  • Expecting dispatch when calling on an object instead of a pointer/reference.
  • A mismatched signature silently hiding instead of overriding (use override).
  • Forgetting a virtual destructor in the base (covered in the main lesson).
🏋️ Practice

Write a base Employee with a virtual salary(), and derived Manager and Intern. Put pointers in an array and print each salary through the base pointer.

Summary

  • virtual goes on the base function; override on the derived.
  • Call through a base pointer/reference for run-time dispatch.
  • = 0 makes a pure virtual and an abstract class.
  • Keyword cheat sheet: virtual, override, =0, final.
  • For internals and design, see the main virtual functions lesson.

Frequently Asked Questions

What is the syntax of a virtual function in C++?
You declare a virtual function by writing virtual before the return type in the base class, such as virtual void draw();. In a derived class you redefine it and add override, like void draw() override;, to have the compiler check the override.
What is a pure virtual function in C++?
A pure virtual function has no body in the base class and is declared by assigning = 0, such as virtual void draw() = 0;. It forces derived classes to provide their own implementation and makes the base class an abstract class that cannot be instantiated.
Do you write virtual on the overriding function in C++?
You do not need to repeat virtual in the derived class; once a function is virtual in the base, its overrides are automatically virtual too. It is good practice to add override instead, which documents intent and lets the compiler catch signature mismatches.
How do you call a virtual function in C++?
You call it like any member function, but for polymorphic behaviour you invoke it through a base-class pointer or reference, such as basePtr->draw();. The version matching the actual object is then selected at run time.
What is the difference between virtual and override in C++?
virtual in the base class marks a function as overridable and enables run-time dispatch. override in a derived class states that the function is intended to override a base virtual, letting the compiler verify the signature and report an error if it does not match.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicPure Virtual & Abstract Class Your progress: 71 / 81 (88%) · Lesson 72 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free