A virtual function in C++ is a base class member function that you redefine in a derived class. The virtual keyword is used to declare it.
It tells the compiler whether the function should be dynamically linked or late-bound.
The use of a single pointer to refer to all of the objects of the various classes is required. As a result, we generate a pointer to the base class that references all derived objects. When the address of the derived class object is contained in the base class pointer, however, the base class function is always executed. The only way to overcome this problem is to use the ‘virtual function.
A ‘virtual’ keyword comes before a function’s usual declaration.
When a function is made virtual, C++ uses the type of object referenced by the base class pointer to determine which function should be invoked at runtime.
Late binding or Dynamic linkage:
The call to a late-binding function is resolved at runtime. As a result, during runtime, the compiler detects the type of object and then binds the function call.
Rules of Virtual Function:
- Virtual functions must belong to a certain class.
- Static members can’t be virtual functions.
- Object pointers are used to access them.
- They could be a friend of another class.
- Even if it isn’t used, a virtual function must be specified in the base class.
- The prototypes of all derived classes and the base class’s virtual functions must be identical. C++ will consider two functions with the same name but different prototypes to be overloaded functions.
- A virtual constructor is not possible, but a virtual destructor is.
- Consider what happens if the virtual keyword isn’t used.
#include <iostream> using namespace std; class A { int x=5; public: void display() { std::cout << "Value of x is : " << x<<std::endl; } }; class B: public A { int y = 10; public: void display() { std::cout << "Value of y is : " <<y<< std::endl; } }; int main() { A *a; B b; a = &b; a->display(); return 0; }
Output:
Value of x is : 5
*a is the base class pointer in the example above. The pointer can only access members of the base class, not those of the derived class.
Despite the fact that C++ allows the base pointer to point to any object derived from the base class, it cannot directly access the derived class’s members. As a result, a virtual function is required to allow the base pointer to access the members of the derived class.
Virtual Function Example In C++:
Let’s look at a simple C++ virtual function that is used to call a derived class in a program.
#include <iostream> class A { public: virtual void display() { cout << "Base class is invoked"<<endl; } }; class B:public A { public: void display() { cout << "Derived Class is invoked"<<endl; } }; int main() { A* a; //pointer of base class B b; //object of derived class a = &b; a->display(); //Late Binding occurs }
Output:
Derived Class is invoked
Pure Virtual Function:
- A virtual function isn’t used to accomplish anything. It’s merely there to be used as a placeholder.
- The term “do-nothing” function refers to a function that has no definition.
- A pure virtual function is a function that does nothing. A pure virtual function is one that is declared in the base class but does not have a definition in the base class.
- Abstract base classes are classes that contain a pure virtual function but cannot be utilised to declare objects of their own.
- The primary goal of the base class is to offer traits to derived classes and to establish the base pointer that allows for runtime polymorphism.
Pure virtual function can be defined as:
virtual void display() = 0;
#include <iostream> using namespace std; class Base { public: virtual void show() = 0; }; class Derived : public Base { public: void show() { std::cout << "Derived class is derived from the base class." << std::endl; } }; int main() { Base *bptr; //Base b; Derived d; bptr = &d; bptr->show(); return 0; }
Output:
Derived class is derived from the base class.
The pure virtual function is contained in the base class in the preceding example. As a result, the base class is referred to as an abstract base class. The object of the base class cannot be created.