Difference between a virtual function and a pure virtual function
In object-oriented programming, virtual functions and pure virtual functions are fundamental concepts for implementing polymorphism. Both are specific to C++ and have several key differences.Virtual FunctionA virtual function is a member function declared in a base class that can be overridden in derived classes. It enables derived classes to redefine or customize the behavior of the base class as needed. When a function is called through a base class pointer or reference, the C++ runtime system ensures that the appropriate derived class function is invoked, demonstrating polymorphism.Example:Suppose there is a base class and two derived classes and . In the class, there is a virtual function , which can be overridden in the and classes.When calling through an pointer or reference, it invokes the appropriate function based on the actual object type.Pure Virtual FunctionA pure virtual function is declared in a base class without any implementation, specified with . A class that declares one or more pure virtual functions is called an abstract class. Abstract classes cannot be instantiated and are used as a base for derived classes.Example:Suppose the class is an abstract concept and should not be instantiated directly. We can declare as a pure virtual function.In this case, any attempt to instantiate an object results in a compilation error, ensuring the purity of the abstract class.SummaryVirtual functions allow derived classes to override base class methods, while pure virtual functions require derived classes to implement the function, enabling stricter abstraction. Virtual functions can have a default implementation, whereas pure virtual functions cannot. By utilizing these concepts, more flexible and robust class hierarchies can be designed, promoting code reuse and extensibility. In C++, both virtual functions and pure virtual functions are used to implement polymorphism, but they have key differences:Virtual Function:A virtual function is a member function that can be overridden in derived classes, declared with the keyword in the base class. When called through a base class pointer or reference, it invokes the appropriate function based on the actual object type, a mechanism known as dynamic binding or late binding.Virtual functions can have a default implementation, meaning the base class provides a basic behavior.Example:In this example, is a virtual function with a default implementation in the base class . When you create a object and call through an reference or pointer, it invokes the function in the class.Pure Virtual Function:A pure virtual function is declared in the base class without any implementation, requiring any non-abstract derived class to provide an implementation. It is declared with .If a class contains at least one pure virtual function, it becomes an abstract class and cannot be instantiated.Example:In this example, is a pure virtual function, making an abstract class that cannot be directly instantiated. All derived classes from (such as ) must implement to be instantiable.Overall, virtual functions allow a default implementation in the base class, while pure virtual functions enforce that derived classes provide an implementation. Both mechanisms support polymorphism, where the same operation can have different implementations on different objects.