Virtual Functions in C++
Introduction
In C++, a virtual function is a member function in the base class that you expect to override in derived classes. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function. Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for function call. They are mainly used to achieve Runtime Polymorphism.
How Virtual Functions Work
Virtual functions use a mechanism known as the vtable (virtual table). A vtable is maintained per class, and it contains pointers to the virtual functions of that class. When a class with virtual functions is instantiated, the vtable pointer is set up for that instance, ensuring the correct function is called at runtime.
Syntax of Virtual Functions
To declare a virtual function, you use the virtual keyword in the base class. Here's the basic syntax:
class Base { public: virtual void show() { cout << "Base class show function called." << endl; } };
Example of Virtual Functions
Consider the following example where we have a base class Base and a derived class Derived. The show function is virtual in the base class and is overridden in the derived class.
#include <iostream> using namespace std; class Base { public: virtual void show() { cout << "Base class show function called." << endl; } }; class Derived : public Base { public: void show() override { cout << "Derived class show function called." << endl; } }; int main() { Base* b; Derived d; b = &d; b->show(); // Output: Derived class show function called. return 0; }
Derived class show function called.
Pure Virtual Functions
A pure virtual function (or abstract function) in C++ is a virtual function that is declared in a base class but has no definition relative to the base class. A pure virtual function is declared by assigning 0 in its declaration. This makes the base class abstract, and you cannot instantiate an object of the base class.
class Base { public: virtual void show() = 0; // Pure virtual function }; class Derived : public Base { public: void show() override { cout << "Derived class show function called." << endl; } };
Example of Pure Virtual Functions
Here is an example demonstrating the use of a pure virtual function:
#include <iostream> using namespace std; class Base { public: virtual void show() = 0; // Pure virtual function }; class Derived : public Base { public: void show() override { cout << "Derived class show function called." << endl; } }; int main() { Base* b; Derived d; b = &d; b->show(); // Output: Derived class show function called. return 0; }
Derived class show function called.
Conclusion
Virtual functions in C++ play a crucial role in achieving runtime polymorphism, allowing you to call derived class methods through base class references or pointers. This makes your code more flexible and easier to manage, especially in large and complex systems. Understanding and effectively using virtual functions and pure virtual functions will significantly enhance your ability to write robust and scalable C++ programs.