How to Create a Pure Virtual Function in C++? Last Updated : 07 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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 declare a member function as a pure virtual function, we can use the following syntax: virtual function_name (arguements) {} = 0; Classes that contain at least one pure virtual function are called abstract classes. If we don't implement the pure virtual function in the derived class, then the derived class also becomes the abstract class and we cannot instantiate it. C++ Program to Implement a Pure Virtual Function C++ // C++ Program to demonstrate how to create a pure virtual // function #include <iostream> using namespace std; // Abstract base class with a pure virtual function class AbstractBase { public: // Pure virtual function virtual void pureVirtualFunction() const = 0; // Non-pure virtual function with an implementation void nonPureVirtualFunction() const { cout << "This is a non-pure virtual function.\n"; } }; // Derived class implementing the pure virtual function class Derived : public AbstractBase { public: // Implement the pure virtual function void pureVirtualFunction() const override { cout << "Derived class implementing the pure " "virtual function.\n"; } }; int main() { // AbstractBase base; // Error: Cannot instantiate an // object of an abstract class // Create an object of the derived class Derived derivedObj; // Call the pure virtual function through the derived // class object derivedObj.pureVirtualFunction(); // Call the non-pure virtual function with an // implementation derivedObj.nonPureVirtualFunction(); return 0; } OutputDerived class implementing the pure virtual function. This is a non-pure virtual function. Comment More infoAdvertise with us Next Article How to Create a Pure Virtual Function in C++? A arivashiscqxq 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 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 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 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 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 to call function within function in C or C++ When we begin programming in C/C++, we generally write one main() function and write all our logic inside this. This approach is fine for very small programs, but as the program size grows, this become unmanageable. So we use functions. We write code in the form of functions. The main function alway 4 min read Like