Virtual Function in C++
A virtual function a member function which is declared within a base class and is re-defined(Overriden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function.
Pure Virtual Functions in C++
A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have an implementation, we only declare it. A pure virtual function is declared by assigning 0 in the declaration.
Similarities between virtual function and pure virtual function
- These are the concepts of Run-time polymorphism.
- Prototype i.e. Declaration of both the functions remains the same throughout the program.
- These functions can’t be global or static.
Difference between virtual function and pure virtual function in C++
|Virtual function||Pure virtual function|
|A virtual function is a member function of base class which can be redefined by derived class.||A pure virtual function is a member function of base class whose only declaration is provided in base class and must defined in derived class.|
|Classes having virtual functions are not abstract.||Base class containing pure virtual function becomes abstract.|
|Definition is given in base class.||No definition is given in base class.|
|Base class having virtual function can be instantiated i.e. its object can be made.||Base class having pure virtual function becomes abstract i.e. it cannot be instantiated.|
|If derived class do not redefine virtual function of base class, then it does not affect compilation.||If derived class do not redefine virtual function of base class, then compilation error occurs.|
|All derived class may or maynot redefine virtual function of base class.||All derived class must redefine pure virtual function of base class.|
- What happens when a virtual function is called inside a non-virtual function in C++
- Pure virtual destructor in C++
- Pure Virtual Functions and Abstract Classes in C++
- Virtual Function in C++
- Default arguments and virtual function
- C++ interview questions on virtual function and abstract class
- Difference between Virtual Machines and Containers
- Difference Between Virtual Private Network (VPN) and Proxy
- Difference between Virtual Private Network (VPN) and Multi-Protocol Label Switching (MPLS)
- Difference between Virtual memory and Cache memory
- Virtual Destructor
- Virtual destruction using shared_ptr in C++
- C++ | Virtual Functions | Question 7
- C++ | Virtual Functions | Question 14
- C++ | Virtual Functions | Question 9
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.