What are virtual functions in C++?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Virtual functions in C++ are a fundamental aspect of Object-Oriented Programming (OOP) that enable runtime polymorphism. A virtual function is a member function in a base class that you can override in a derived class. When you call a virtual function on a base class pointer or reference, C++ determines which version of the function to execute based on the type of the object being referenced rather than the type of the pointer or reference. This mechanism is crucial for achieving dynamic binding and allows for more flexible and reusable code.

Key Features of Virtual Functions:

  1. Declared with virtual Keyword: A function is declared as virtual in the base class using the virtual keyword. This indicates that it can be overridden in derived classes.

    class Base { public: virtual void show() { cout << "Base class show function called." << endl; } };
  2. Dynamic Binding: Virtual functions support dynamic (or late) binding, meaning the decision about which function to call is made at runtime based on the actual object type, not the type of the pointer/reference.

  3. Overriding: Derived classes can provide their own implementation of the virtual function, which will be called when the function is invoked on a base class pointer or reference that points to a derived class object.

    class Derived : public Base { public: void show() override { // 'override' is optional but improves clarity cout << "Derived class show function called." << endl; } };
  4. Base Class Pointer to Derived Class Object: When a base class pointer points to a derived class object and calls a virtual function, the derived class's version of the function is executed.

    Base* b; // Base class pointer Derived d; // Derived class object b = &d; // Pointing to derived class object b->show(); // Calls Derived's show function

Example of Virtual Functions:

Here's a simple example to illustrate the concept of virtual functions:

#include <iostream> using namespace std; class Base { public: virtual void show() { // Virtual function cout << "Base class show function called." << endl; } }; class Derived : public Base { public: void show() override { // Overriding the base class function cout << "Derived class show function called." << endl; } }; int main() { Base* b; // Base class pointer Derived d; // Derived class object b = &d; // Pointing to derived class object b->show(); // Calls Derived's show() function return 0; }

Output:

Derived class show function called.

Use Cases of Virtual Functions:

  • Polymorphism: Virtual functions enable polymorphism, allowing different classes to be treated as instances of the same class through a base class interface.
  • Flexibility: They allow for extending the functionality of base classes without modifying existing code.
  • Interface Implementation: Virtual functions are used to define interfaces in C++, allowing for a flexible design where derived classes implement specific behaviors.

Conclusion:

Virtual functions are a powerful feature in C++ that supports dynamic polymorphism and enhances the flexibility of the object-oriented design. They allow derived classes to override base class methods, enabling more dynamic and extensible software architecture.

Sources:

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
How to clear a Google interview?
What is the minimum CGPA for Atlassian?
How do I prepare for LinkedIn?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.