How to Create a Virtual Function in C++? Last Updated : 22 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, a virtual function is a member function that is declared in a base class and redefined in a derived class. It enables runtime polymorphism in our program. In this article, we will learn how to create a virtual function in C++. Creating a Virtual Function in C++We can use the virtual keyword to declare a function as virtual in the base class which is then overridden in a derived class. Syntax to Declare a Virtual FunctionThe syntax for declaring a virtual function within the parent class is as follows: virtual ReturnType FunctionName(Parameters){ // some body statements};We can then redefine this function in the derived class. C++ Program to Create a Virtual FunctionThe below example demonstrates how we can create a virtual function using a virtual keyword in C++. C++ // C++ Program to create a Virtual function #include <iostream> using namespace std; // Base class class Shape { public: // Virtual function virtual void draw() { cout << "Drawing a shape." << endl; } }; // Derived class class Circle : public Shape { public: // Override the virtual function void draw() override { cout << "Drawing a circle." << endl; } }; int main() { // Create objects Shape* shapeptr; Circle circle; shapeptr = &circle; // Virtual function, binded at runtime shapeptr->draw(); return 0; } OutputDrawing a circle. Time Complexity: O(1)Space Complexity: O(1) Note: We can also declare the function as pure virtual function by using the syntax: virtual func() = 0; Comment More infoAdvertise with us Next Article How to Call a Virtual Function From a Derived Class in C++? S santhoshchekuri2002 Follow Improve Article Tags : C++ Programs C++ C++-Virtual Functions CPP-OOPs CPP Examples +1 More 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 Function Template in C++? In C++, templates enable us to write generic programs that handle any data type. We can create a template class, function, and variable. A template function is a function that can work with any data type. In this article, we will learn how to create a function template in C++. Create a Function Temp 2 min read How to Create a Pointer to a Function in C++? In C++, a function pointer is a variable that stores the address of a function that can later be called through that function pointer. It is useful for passing functions as parameters to other functions(callback functions) or storing them in data structures. In this article, we will learn how to use 2 min read How to Call a Virtual Function From a Derived Class in C++? 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 vir 2 min read How Do I Create a Library in C++? Libraries are reusable code packages that can be imported into our program to use the code defined in them. In C++, libraries can be either static or dynamic. A static library is a library that is linked to the program at compile-time whereas dynamic libraries in C++ are linked at runtime but they h 4 min read How to Return a Vector From a Function in C++? In C++, by returning vectors, users to return multiple values at once from a function as the elements of vector. In this article, we will learn how to return a vector from a function in C++.The recommended way to return a vector from a function is by using return vector by value method. Letâs take a 3 min read Like