-
C++ virtual functions are member functions that are redefined in derived classes.
-
They are declared using the virtual keyword. This is used to inform the compiler to perform dynamic linking or late binding on the function.
-
It is necessary to use a single pointer to reference all objects of different classes. Thus, we create a pointer to the base class that references all derived objects. However, when the base class pointer contains the address of a derived class object, the base class function is always executed. This issue can only be resolved using virtual functions.
-
The virtual keyword precedes the declaration of a regular function.
-
When a function is declared as a virtual function, C++ determines which function to call at runtime based on the type of the object pointed to by the base class pointer.
Late Binding or Dynamic Linking
Late binding function calls are resolved at runtime. Therefore, the compiler determines the type of the object at runtime and then binds the function call.
👇 Click to Claim 👇
👉 C Language Knowledge Material Collection
Rules for Virtual Functions
-
Virtual functions must be members of a class.
-
Virtual functions cannot be static members.
-
They are accessed via object pointers.
-
They can be friends of another class.
-
Virtual functions must be defined in the base class, even if they are not used. The prototypes of virtual functions in the base class and all derived classes must be the same. If two functions have the same name but different prototypes, C++ will consider them overloaded functions.
-
There cannot be virtual constructors, but there can be virtual destructors.
-
Consider the situation when we do not use the virtual keyword.
#include <iostream>
using namespace std;
class A{ int x=5; public: void display(){ std::cout << "Value of x: " << x << std::endl; }};
class B: public A{ int y = 10; public: void display(){ std::cout << "Value of y: " << y << std::endl; }};
int main(){ A *a; B b; a = &b a->display(); return 0;}
Output:
Value of x: 5
In the example above, *a is a base class pointer. The pointer can only access base class members and cannot access derived class members. Although C++ allows base pointers to point to any object derived from the base class, it cannot directly access the members of the derived class. Therefore, virtual functions are needed to allow base pointers to access derived class members.
Example of C++ Virtual Function
Let’s look at a simple example of using C++ virtual functions to call derived classes in the program.
#include <iostream>
using namespace std;
class A{ public: virtual void display(){ cout << "Base class called" << endl; }};
class B:public A{ public: void display(){ cout << "Derived class called" << endl; }};
int main(){ A* a; // Base class pointer B b; // Derived class object a = &b a->display(); // Late binding occurs
}
Output:
Derived class called
Pure Virtual Functions
-
Virtual functions are not used to perform any tasks. They only act as placeholders.
-
When a function is not defined, such a function is called a “do-nothing” function.
-
Such a “do-nothing” function is called a pure virtual function. A pure virtual function is one that is declared in the base class without a definition related to the base class.
-
A class containing pure virtual functions cannot be used to declare its own objects, such a class is called an abstract base class.
-
The main purpose of the base class is to provide features for derived classes and to create a base pointer used to implement runtime polymorphism.
A pure virtual function can be defined as:
virtual void display() = 0;
Let’s look at a simple example:
#include <iostream>
using namespace std;
class Base{ public: virtual void show() = 0;};
class Derived : public Base{ public: void show(){ std::cout << "Derived class derived from base class." << std::endl; }};
int main(){ Base *bptr; //Base b; Derived d; bptr = &d bptr->show(); return 0;}
Output:
Derived class derived from base class.
In the example above, the base class contains a pure virtual function. Therefore, the base class is an abstract base class. We cannot create objects of the base class.
Popular Recommendations
-
CLion Tutorial – Change Project Root Directory in CLion
-
C Language Algorithm – “Maximum Rectangle” Algorithm Problem
-
C++ Tutorial – Detailed Explanation of C++ Language Overloading (Functions and Operators)