---
title: "Need of virtual function"
subject: "Object Oriented Programming"
chapter: 7
key: "semester_3/object_oriented_programming:virtual-function-need"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/virtual-function-need
site: BCT Engineering
---

# Need of virtual function

Object Oriented Programming · Chapter 7: Polymorphism and Dynamic Binding

- Asked in: 2072 Kartik, 5 marks; 2070 Asar, 2 marks; 2070 Chaitra, 6 marks; 2069 Chaitra, 5 marks (asked 4 times)
- Video: [Virtual Functions in C++](https://www.youtube.com/watch?v=oIV2KchSyGQ) — The Cherno
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/virtual-function-need
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-7

## Question

Why do we need virtual function? Explain with suitable example.

## Short answer

Virtual functions allow a base class pointer to call the derived class's overridden function at run time, enabling polymorphism. Without virtual, the base version is called even when the object is derived. Example: virtual void show() in base ensures p->show() executes the derived's version.

## Answer

A **virtual function** is a member function declared with the keyword `virtual` in the base class and redefined in derived classes. It makes the compiler choose the function version at **run time** based on the type of the object pointed to, not on the type of the pointer.

#### Why we need it
When a base class pointer holds the address of a derived class object, a normal (non-virtual) function call is bound at compile time to the **base** version, because the pointer is of the base type. The derived version never runs, which is wrong when we want derived behaviour. Declaring the function `virtual` fixes this and gives **run-time polymorphism** (late binding). It lets us:
- write one piece of code that works with all classes in a hierarchy (e.g. an array of `Shape*` holding circles and rectangles);
- add new derived classes without changing the code that uses the base pointer.

#### Example without virtual

```cpp
#include <iostream>
using namespace std;

class Base {
public:
    void show() { cout << "Base show" << endl; }
};
class Derived : public Base {
public:
    void show() { cout << "Derived show" << endl; }
};

int main() {
    Base *p;
    Derived d;
    p = &d;
    p->show();       // prints "Base show" - not wanted
    return 0;
}
```

#### Example with virtual

```cpp
#include <iostream>
using namespace std;

class Base {
public:
    virtual void show() { cout << "Base show" << endl; }
};
class Derived : public Base {
public:
    void show() { cout << "Derived show" << endl; }
};

int main() {
    Base *p;
    Derived d;
    p = &d;
    p->show();       // prints "Derived show"
    return 0;
}
```

**Output:** `Derived show`

With `virtual`, the call `p->show()` uses the actual object (`Derived`) to pick the function through the vtable.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. A Base pointer points to a Derived object and show() is non-virtual. Which version runs?
   A. Derived::show()
   B. Both versions
   C. Base::show()
   D. A compile error
   Answer: C. Non-virtual calls are bound at compile time to the pointer's type, so the base version runs. (Key point: Problem without virtual)

2. What does declaring show() virtual in Base change?
   A. The derived class gets no copy of it
   B. The version is chosen at compile time by the pointer type
   C. The version is chosen at run time by the actual object type
   D. The function can no longer be redefined
   Answer: C. Virtual gives late binding: the actual object decides which function runs. (Key point: What virtual does)

3. Which is a benefit of virtual functions mentioned in the answer?
   A. Programs run faster than with static binding
   B. New derived classes can be added without changing code that uses the base pointer
   C. Objects use less memory
   D. Constructors become virtual
   Answer: B. One piece of code using base pointers works for all current and future derived classes. (Key point: Benefit)

4. How does the run-time choice happen internally?
   A. Through the linker
   B. Through a macro expansion
   C. Through the virtual table (vtable)
   D. Through a friend function
   Answer: C. Objects carry a vptr to a vtable of function addresses used for dynamic binding. (Key point: Mechanism)

## Related in Object Oriented Programming

- [Need of virtual base class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avirtual-base-class) — Object Oriented Programming, chapter 6
- [Pure virtual function and abstract class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apure-virtual-abstract-class) — Object Oriented Programming, chapter 7, asked 5×
- [Member function overriding with virtual function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avirtual-member-override-reason) — Object Oriented Programming, chapter 7
- [Need of friend function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afriend-function-need) — Object Oriented Programming, chapter 4, asked 2×
- [Need of template; function template overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Awhy-template-function-overloading) — Object Oriented Programming, chapter 9
- [Virtual destructor with example](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avirtual-destructor) — Object Oriented Programming, chapter 7

## Related in other subjects

- [Why do we need DFT](https://www.bctengineer.com/question.md?key=semester_7%2Fdigital_signal_analysis_and_processing%3Awhy-need-dft) — Digital Signal Analysis and Processing, chapter 7, asked 6×
- [Why do we need FFT](https://www.bctengineer.com/question.md?key=semester_7%2Fdigital_signal_analysis_and_processing%3Awhy-need-fft) — Digital Signal Analysis and Processing, chapter 7, asked 2×
- [Need for difference equation; LCCDE and system function](https://www.bctengineer.com/question.md?key=semester_7%2Fdigital_signal_analysis_and_processing%3Awhy-difference-equation-lccde) — Digital Signal Analysis and Processing, chapter 1, asked 2×
- [Firewall, its need and types](https://www.bctengineer.com/question.md?key=semester_7%2Fcomputer_networks_and_security%3Afirewall-types-working) — Computer Networks and Security, chapter 8, asked 7×
