How to Call a Virtual Function From a Derived Class in C++? Last Updated : 30 Apr, 2024 Comments Improve Suggest changes Like Article Like Report In C++, virtual functions play an important role because they allow the users to perform run-time polymorphism. While dealing with inheritance and virtual functions, it is very crucial to understand how to call a virtual function from a derived class. In this article, we will learn how to call a virtual function from a derived class in C++. Call a Virtual Function From a Derived Class in C++ A virtual function is a member function that is defined in a base class which is later overridden in a derived class. When we call a virtual function through a base class pointer or reference the current most derived class function which will be executed is determined during the runtime of the program. We can call the base class virtual function by using the scope resolution operator and class name. BaseClassName::function_name(args);C++ program to Call a Virtual Function From a Derived Class The following program demonstrates how we can call a virtual function from a derived class in C++: C++ // C++ program to Call a Virtual Function From a Derived Class #include <iostream> using namespace std; // Base class class Base { public: // Declaring the virtual function virtual void display() { cout << "Base class virtual function called" << endl; } }; // Derived class class Derived : public Base { public: // override the base class virtual function void display() override { cout << "Derived class function called" << endl; } // Function to call the virtual function from the derived class void call() { // Calling the virtual function from the derived class Base::display(); } }; int main() { // Declare an object of the derived class Derived derivedObj; // Call Derived class display() derivedObj.display(); // Call Base class virtual function from Derived class derivedObj.call(); return 0; } OutputDerived class function called Base class virtual function called Time Complexity:O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Call a Virtual Function From a Derived Class in C++? rohitsharma_26 Follow Improve Article Tags : C++ Programs C++ CPP-OOPs CPP Examples Practice Tags : CPP Similar Reads How to Create a Pure Virtual Function in C++? In C++, pure virtual functions are those functions that are not implemented in the base class. They are instead implemented in the derived classes if necessary. In this article, we will discuss how to create a pure virtual function in a class in C++. How to Create a Pure Virtual Function in C++? To 2 min read How to Create a Derived Class from a Base Class in C++? In C++, one of the fundamental concepts of Object Oriented Programming (OOPS) is inheritance, allowing users to create new classes based on existing classes. The class that inherits another class is called derived class and the class that is inherited is called base class. In this article, we will l 2 min read Behavior of virtual function in the derived class from the base class and abstract class In this article, we will discuss the behavior of Virtual Function in the derived class and derived class from the Abstract Base Class in C++.Consider the following program:C++ // C++ program to illustrate the concept // of Virtual Function #include <bits/stdc++.h> using namespace std; // Base 3 min read How to Declare a Static Member Function in a Class in C++? In C++, static functions are functions that are directly associated with a class so we can access the static function directly without creating an object of the class using the scope resolution operator. In this article, we will learn how we can declare a static function in a class in C++. Declare a 1 min read How to Override a Base Class Method in a Derived Class in C++? Function overriding is a concept in object-oriented programming languages, in which a function/method of a parent class is redefined in its child class to change its behavior for the objects of the child class. In this article, we are going to learn how to override a base class function in a derived 2 min read Like