---
title: "What is the need for virtual functions in C++?"  
description: "What is the need for virtual functions in C++?"  
author: "Utpal Vishwas"  
published: 2023-07-07  
updated: 2023-07-10  
canonical: https://www.mindstick.com/forum/158994/what-is-the-need-for-virtual-functions-in-c-plus-plus  
category: "visual c++"  
tags: ["c++", "programming language"]  
reading_time: 2 minutes  

---

# What is the need for virtual functions in C++?

What is the need for [virtual functions](https://www.mindstick.com/forum/159480/explain-the-purpose-of-the-virtual-keyword-and-virtual-functions-in-c-plus-plus) in [C++](https://www.mindstick.com/blog/745/array-in-c-plus-plus)?

## Replies

### Reply by Aryan Kumar

[Virtual](https://yourviews.mindstick.com/view/81341/the-oh-so-fight-for-democracy-virtual-rally-scenes) [functions](https://www.mindstick.com/forum/160140/explain-the-role-of-functions-as-a-service-faas-in-serverless-computing) are needed in C++ to allow polymorphism, which is the ability of an object to behave differently depending on its type.

Polymorphism is achieved by making a function virtual. A virtual function is a function that is declared in a base class and can be overridden in a derived class. Overriding a function means providing a different implementation of the function in the derived class.

When a virtual function is called on an object, the compiler will look for the most derived version of the function that is applicable to the object. For example, if a `Derived` class object calls a virtual function that is also defined in the `Base` class, the compiler will call the `Derived` class version of the function.

Virtual functions are used in a variety of situations, such as:

- **Abstract classes:** An abstract class is a class that cannot be instantiated. Abstract classes are used to define the common behavior of a set of classes. The virtual functions in an abstract class are implemented in the derived classes.
- **Interfaces:** An interface is a set of method declarations that a class must implement. Interfaces are used to define the behavior of a class without specifying how the behavior is implemented. The virtual functions in an interface are implemented in the classes that implement the interface.
- **Polymorphism:** As mentioned above, polymorphism is the ability of an object to behave differently depending on its type. Virtual functions are used to achieve polymorphism by allowing the compiler to call the most derived version of a function depending on the type of the object that is calling the function.


---

Original Source: https://www.mindstick.com/forum/158994/what-is-the-need-for-virtual-functions-in-c-plus-plus

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
