---
title: "What is virtual function and why is it used? with example."  
description: "What is virtual function and why is it used? with example."  
author: "ICSM Computer"  
published: 2025-07-01  
updated: 2025-11-07  
canonical: https://www.mindstick.com/forum/161768/what-is-virtual-function-and-why-is-it-used-with-example  
category: "programming language"  
tags: ["c++"]  
reading_time: 3 minutes  

---

# What is virtual function and why is it used? with example.

**What is [virtual function](https://www.mindstick.com/interview/34/what-is-pure-virtual-function) and why is it used? with example.**

## Replies

### Reply by Anubhav Sharma

> A **[virtual](https://yourviews.mindstick.com/view/81341/the-oh-so-fight-for-democracy-virtual-rally-scenes) [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server)** is a function in a base class that can be **overridden in derived classes**.\
> It allows **runtime polymorphism** — meaning the **function that gets called is determined at runtime** based on the **type of object** being referred to, not the type of pointer or reference.

### Why it is used

Virtual functions are used to:

- **Achieve runtime polymorphism (dynamic binding)**.
- **Allow derived classes to provide their own implementation** of a base class method.
- **Make code flexible and extensible** — you can add new derived types without changing existing code.

### Syntax

## In C++:

```cpp
class Base {
public:
    virtual void show() {    // virtual function
        cout << "Base class show function" << endl;
    }
};

class Derived : public Base {
public:
    void show() override {   // override keyword is optional
        cout << "Derived class show function" << endl;
    }
};
```

### Example — Without and With Virtual Function

#### Without virtual function:

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

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

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

int main() {
    Base* ptr;
    Derived obj;
    ptr = &obj;

    ptr->show();  // Calls Base::show() because it's not virtual
    return 0;
}
```

## Output:

```plaintext
Base class show
```

#### With virtual function:

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

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

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

int main() {
    Base* ptr;
    Derived obj;
    ptr = &obj;

    ptr->show();  // Calls Derived::show() because of virtual function
    return 0;
}
```

## Output:

```plaintext
Derived class show
```

### Key Points:

- Virtual functions must be **declared in the base class** using the `virtual` keyword.
- They **enable late binding** (runtime decision).
- If a class has at least one virtual function, it should have a **virtual destructor** to ensure proper cleanup.
- The `override` keyword (C++11 onward) ensures that the function actually overrides a base class virtual function.
- Virtual functions are implemented internally using a **vtable** (virtual table).


---

Original Source: https://www.mindstick.com/forum/161768/what-is-virtual-function-and-why-is-it-used-with-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
