Virtual Function vs. Pure Virtual Function — What's the Difference?
By Tayyaba Rehman — Published on January 9, 2024
Virtual Function is a function in a base class that can be overridden by a derived class. Pure Virtual Function is a function in a base class with no implementation, requiring derived classes to provide an implementation.
Difference Between Virtual Function and Pure Virtual Function
Table of Contents
ADVERTISEMENT
Key Differences
Virtual Function: A virtual function is a member function in a base class that you expect to redefine in derived classes. It allows for dynamic polymorphism, letting you use base class pointers to call derived class functions. Pure Virtual Function: Conversely, a pure virtual function is a function declared in a base class with no implementation and marked with "= 0". It makes the base class abstract, mandating that derived classes must override this function to be instantiable.
Virtual Function: Virtual functions are typically used when the base class has a default implementation that might be sufficient for some derived classes but not all. They support runtime polymorphism and are integral to achieving late binding. Pure Virtual Function: Pure virtual functions are used when the base class cannot provide a meaningful implementation for the function, thus enforcing that the derived classes provide their own specific implementation.
Virtual Function: In scenarios where a base class's function has a meaningful default implementation, virtual functions are ideal. They allow derived classes to either use the default implementation or provide a specialized one. Pure Virtual Function: When the function's behavior cannot be generalized, pure virtual functions are used. They're crucial in scenarios where every derived class must have its own implementation of the function, reflecting its unique characteristics.
Virtual Function: Virtual functions maintain class hierarchy integrity by allowing overridable functions. This flexibility benefits scenarios where behaviors are similar but not identical across subclasses. Pure Virtual Function: Pure virtual functions are used to create interfaces and abstract classes in C++. They are instrumental in designing frameworks and APIs where the implementation specifics are left to the user, ensuring a standardized interface.
Virtual Function: Virtual functions are a cornerstone of object-oriented programming, providing extensibility and flexibility in code design. They exemplify the concept of polymorphism. Pure Virtual Function: Pure virtual functions take polymorphism a step further by not just allowing but enforcing variation in behavior across derived classes, making them essential for abstract base classes and interface-like constructs.
ADVERTISEMENT
Comparison Chart
Definition
A function that can be overridden in derived class
A function with no implementation, must be overridden
Usage
Provides default behavior, overridable
Forces derived classes to provide an implementation
Class Type
Used in both concrete and abstract classes
Makes a class abstract
Polymorphism
Supports runtime polymorphism
Enforces compile-time contract for subclasses
Implementation Requirement
Optional for derived classes
Mandatory for derived classes
Compare with Definitions
Virtual Function
A virtual function is an overridable function defined in a base class.
In a base class 'Animal', a virtual function 'speak()' allows different 'Animal' subclasses to have their own sound.
Pure Virtual Function
A pure virtual function is a function without implementation, needing override in derived classes.
An abstract class 'UIComponent' has a pure virtual function 'draw()', which must be implemented in 'Button'.
Virtual Function
A virtual function provides a common interface with potentially different implementations.
'calculateArea()' defined as a virtual function in 'Shape' can be implemented differently in 'Rectangle'.
Pure Virtual Function
Pure virtual functions create abstract base classes in C++.
In 'Database', a pure virtual function 'connect()' ensures all derived database types implement their connection method.
Virtual Function
Virtual functions are member functions that can be redefined in derived classes.
In 'Vehicle', a virtual function 'fuelType()' can be overridden in 'ElectricCar' to return Electric.
Pure Virtual Function
Pure virtual functions enforce a contract for subclasses to implement specific functions.
'serialize()' in an interface 'Serializable' is a pure virtual function, requiring all implementers to define serialization logic.
Virtual Function
Virtual functions facilitate late binding in C++.
A call to 'render()' in a base class 'GraphicObject' could actually call 'render()' in 'Image' or 'Text'.
Pure Virtual Function
A pure virtual function is used to define an interface in object-oriented programming.
In an interface 'Movable', a pure virtual function 'move()' requires all movable objects to provide a move behavior.
Virtual Function
Virtual functions enable runtime polymorphism in object-oriented programming.
Calling 'draw()' on a base class pointer 'Shape' can invoke the appropriate 'draw()' in 'Circle' or 'Square'.
Pure Virtual Function
Pure virtual functions are declarations without definitions in a base class.
'play()' in an abstract class 'Instrument' is pure virtual, making subclasses like 'Guitar' define how to play.
Common Curiosities
Can a pure virtual function have an implementation?
No, a pure virtual function cannot have an implementation in the base class; it must be implemented by the derived classes.
What makes a function pure virtual?
A function becomes pure virtual when it's declared with "= 0" in the base class, indicating it has no default implementation.
Is it mandatory to override a pure virtual function?
Yes, it is mandatory to override a pure virtual function in any concrete subclass.
What happens if a virtual function is not overridden?
If a virtual function is not overridden, the base class version of the function is used.
What is the purpose of a virtual function?
The purpose of a virtual function is to allow derived classes to provide specific implementations for functions defined in the base class.
What is a virtual function?
A virtual function is a function in a base class that can be overridden by derived classes for customized behavior.
Can a virtual function be private?
Yes, a virtual function can be private, but it's still overridable by derived classes.
Can a derived class override a pure virtual function with another pure virtual function?
Yes, a derived class can override a pure virtual function with another pure virtual function, keeping the class abstract.
How does a virtual function support polymorphism?
Virtual functions support polymorphism by allowing base class pointers to call derived class methods.
What is an abstract class?
An abstract class is a class with at least one pure virtual function.
Can a constructor be virtual or pure virtual?
No, constructors cannot be virtual or pure virtual in C++.
Why use pure virtual functions?
Pure virtual functions are used to create an interface or abstract class, enforcing a contract for behavior in derived classes.
Can a class with a pure virtual function be instantiated?
No, classes with pure virtual functions are considered abstract and cannot be instantiated.
Do pure virtual functions improve code modularity?
Yes, pure virtual functions improve modularity by separating interface from implementation.
How does a destructor work in a class with virtual functions?
A destructor should be virtual in a class with virtual functions to ensure proper resource cleanup in derived classes.
Share Your Discovery
Previous Comparison
Golden Retriever vs. Labrador RetrieverNext Comparison
Progressively vs. SlowlyAuthor Spotlight
Written by
Tayyaba RehmanTayyaba Rehman is a distinguished writer, currently serving as a primary contributor to askdifference.com. As a researcher in semantics and etymology, Tayyaba's passion for the complexity of languages and their distinctions has found a perfect home on the platform. Tayyaba delves into the intricacies of language, distinguishing between commonly confused words and phrases, thereby providing clarity for readers worldwide.