Difference Between
versus

Virtual Function vs. Pure Virtual Function: Know the Difference

Hifza Nasir
By Hifza Nasir & Dua Fatima || Published on March 5, 2024
Virtual functions in C++ allow derived classes to override a base class function, while pure virtual functions make a class abstract, requiring derived classes to provide an implementation.
Virtual Function vs. Pure Virtual Function

Key Differences

Virtual functions in C++ are member functions that can be overridden in derived classes, enabling polymorphism. This allows for dynamic dispatch, where the function called is determined at runtime based on the object's type. Pure virtual functions, declared with "= 0" in their declaration, serve to create abstract classes. These classes cannot be instantiated directly and compel derived classes to implement the pure virtual function, ensuring a specific interface is followed.
Dua Fatima
Dua Fatima
Mar 05, 2024
By defining a virtual function in a base class, you enable functionality that can be extended or modified in derived classes. This provides flexibility in how classes interact, making code more modular and reusable. Conversely, a pure virtual function does not provide a default implementation, pushing the responsibility of defining the function's behavior onto the derived class, which can ensure that all derived classes adhere to a uniform interface.
Hifza Nasir
Hifza Nasir
Mar 05, 2024
Virtual functions are used when a base class has a default implementation that might be sufficient for some derived classes. This allows derived classes to use the base class implementation or override it with their own. In contrast, pure virtual functions are used when there is no meaningful default behavior that the base class can provide, making it essential for the derived class to provide its own implementation.
Dua Fatima
Dua Fatima
Mar 05, 2024
The concept of virtual functions enables C++ to support runtime polymorphism, an essential feature of object-oriented programming. This allows for more flexible and dynamic code structures. Pure virtual functions, on the other hand, are a key component in designing abstract base classes and interfaces, guiding the architectural structure of complex systems by mandating certain behaviors in the derived classes.
Hifza Nasir
Hifza Nasir
Mar 05, 2024
Virtual and pure virtual functions play crucial roles in the implementation of polymorphism and inheritance in C++. While virtual functions provide a mechanism for dynamic behavior change, pure virtual functions enforce a contract for derived classes, highlighting the blend of flexibility and structure in object-oriented design.
Dua Fatima
Dua Fatima
Mar 05, 2024
ADVERTISEMENT

Comparison Chart

Definition

A function that can be overridden by derived classes.
A function that must be implemented by derived classes.
Hifza Nasir
Hifza Nasir
Mar 05, 2024

Syntax

Virtual void functionName();
Virtual void functionName() = 0;
Dua Fatima
Dua Fatima
Mar 05, 2024

Purpose

To allow derived classes to modify or extend base class functionality.
To make a class abstract and ensure that derived classes provide an implementation.
Hifza Nasir
Hifza Nasir
Mar 05, 2024

Instantiation

Classes with virtual functions can be instantiated.
Classes with pure virtual functions cannot be instantiated.
Dua Fatima
Dua Fatima
Mar 05, 2024

Usage

When a default implementation may be overridden.
When there is no meaningful default implementation.
Hifza Nasir
Hifza Nasir
Mar 05, 2024
ADVERTISEMENT

Virtual Function and Pure Virtual Function Definitions

Virtual Function

A virtual function allows derived classes to override it.
Virtual void display() { cout << Base class display; }.
Hifza Nasir
Hifza Nasir
Feb 26, 2024

Pure Virtual Function

Facilitates strict polymorphism.
Enforces behavior uniformity across derived classes.
Shumaila Saeed
Shumaila Saeed
Feb 26, 2024

Virtual Function

It supports runtime polymorphism.
Base* ptr = new Derived(); ptr->display().
Dua Fatima
Dua Fatima
Feb 26, 2024

Pure Virtual Function

It makes the class abstract.
Class with pure virtual function cannot be instantiated.
Hifza Nasir
Hifza Nasir
Feb 26, 2024

Virtual Function

Provides a default implementation that can be used or replaced.
Derived class did not override display, Base's version is used.
Dua Fatima
Dua Fatima
Feb 26, 2024
ADVERTISEMENT

Pure Virtual Function

Ensures derived classes follow a specific interface.
Derived class must implement the display function.
Hifza Nasir
Hifza Nasir
Feb 26, 2024

Virtual Function

Enables dynamic dispatch in C++.
Virtual function call is resolved at runtime.
Hifza Nasir
Hifza Nasir
Feb 26, 2024

Pure Virtual Function

Used in abstract base classes and interfaces.
Defines a contract for derived classes.
Dua Fatima
Dua Fatima
Feb 26, 2024

Virtual Function

Used when base class provides a common interface.
All derived classes can use or modify the display function.
Dua Fatima
Dua Fatima
Feb 26, 2024

Pure Virtual Function

A pure virtual function requires an override in derived classes.
Virtual void display() = 0.
Dua Fatima
Dua Fatima
Feb 26, 2024

Repeatedly Asked Queries

What is a virtual function in C++?

A virtual function is a member function in a base class that can be overridden by derived classes to provide specialized behavior.
Dua Fatima
Dua Fatima
Mar 05, 2024

What is the role of pure virtual functions?

Pure virtual functions define an interface that all derived classes must implement, making the base class abstract.
Hifza Nasir
Hifza Nasir
Mar 05, 2024

Can a virtual function have an implementation?

Yes, a virtual function can have an implementation in the base class, providing a default behavior that derived classes can override.
Dua Fatima
Dua Fatima
Mar 05, 2024

What happens if a derived class doesn't override a pure virtual function?

If a derived class doesn't override a pure virtual function, it remains abstract and cannot be instantiated.
Dua Fatima
Dua Fatima
Mar 05, 2024

Why might a class have both virtual and pure virtual functions?

A class might have both to provide some default behaviors while also requiring certain functions to be implemented by derived classes, creating a mix of flexibility and strict interface adherence.
Dua Fatima
Dua Fatima
Mar 05, 2024

What makes a function pure virtual?

A function becomes pure virtual when it is declared with "= 0" in its declaration, indicating it must be implemented by derived classes.
Hifza Nasir
Hifza Nasir
Mar 05, 2024

What is an abstract class in C++?

An abstract class is a class that cannot be instantiated and typically contains one or more pure virtual functions, serving as a base for other classes.
Shumaila Saeed
Shumaila Saeed
Mar 05, 2024

Can a class with a pure virtual function be instantiated?

No, classes with pure virtual functions are considered abstract and cannot be instantiated.
Hifza Nasir
Hifza Nasir
Mar 05, 2024

Why use virtual functions?

Virtual functions allow for dynamic polymorphism, enabling runtime selection of function implementations based on the object type.
Shumaila Saeed
Shumaila Saeed
Mar 05, 2024

How do virtual functions support polymorphism?

Virtual functions support polymorphism by allowing derived classes to override base class functions, enabling dynamic behavior based on the object's actual type.
Dua Fatima
Dua Fatima
Mar 05, 2024

Is it possible to have a pure virtual destructor?

Yes, a destructor can be pure virtual, requiring a derived class to provide an implementation, though the base class must also provide a definition.
Hifza Nasir
Hifza Nasir
Mar 05, 2024

Can a class be derived from an abstract class without implementing its pure virtual functions?

Yes, but the derived class will also be abstract and cannot be instantiated until it provides implementations for all inherited pure virtual functions.
Dua Fatima
Dua Fatima
Mar 05, 2024

How do constructors work with virtual and pure virtual functions?

Constructors cannot be virtual or pure virtual. They are used to initialize objects and do not participate in polymorphism.
Hifza Nasir
Hifza Nasir
Mar 05, 2024

Can a pure virtual function have a body?

Yes, a pure virtual function can have a body defined outside the class declaration, though it's uncommon.
Hifza Nasir
Hifza Nasir
Mar 05, 2024

How do virtual and pure virtual functions differ in use?

Virtual functions are used when there's a default behavior that might be sufficient or needs to be extended, while pure virtual functions are used to enforce a contract for derived classes to implement specific behavior.
Hifza Nasir
Hifza Nasir
Mar 05, 2024

Share this page

Link for your blog / website
HTML
Link to share via messenger
About Author
Hifza Nasir
Written by
Hifza Nasir
Dua Fatima
Co-written by
Dua Fatima

Popular Comparisons

Trending Comparisons

Hydroscopic vs. HygroscopicHydroscopic vs. Hygroscopic
Shumaila SaeedShumaila Saeed
February 14, 2024
Hydroscopic is a common misnomer, often incorrectly used in place of hygroscopic. Hygroscopic refers to substances that absorb moisture from the air.
Stuck vs. StockStuck vs. Stock
Shumaila SaeedShumaila Saeed
June 18, 2024
"Stuck" refers to being unable to move or progress, while "stock" primarily denotes inventory or shares in a company, highlighting distinct usage contexts.
Polo Ralph Lauren vs. US Polo AssnPolo Ralph Lauren vs. US Polo Assn
Shumaila SaeedShumaila Saeed
January 21, 2024
Polo Ralph Lauren is a premium fashion brand known for luxury clothing, while US Polo Assn is the official brand of the United States Polo Association, focused on affordable casual wear.
Pulley vs. SheavePulley vs. Sheave
Hifza NasirHifza Nasir
April 4, 2024
A pulley is a wheel on an axle designed to support movement and change of direction of a taut cable, while a sheave is the wheel part of a pulley system that specifically interacts with the cable.
Pycharm Community vs. Pycharm ProPycharm Community vs. Pycharm Pro
Shumaila SaeedShumaila Saeed
February 4, 2024
PyCharm Community is a free, open-source IDE for Python development, while PyCharm Pro is a paid version with additional advanced features like web development support and database tools.
Catholic Bible vs. NIV BibleCatholic Bible vs. NIV Bible
Shumaila SaeedShumaila Saeed
February 11, 2024
The Catholic Bible includes additional books in the Old Testament not found in the NIV Bible; the NIV is a modern English translation.
Natural Rubber vs. Synthetic RubberNatural Rubber vs. Synthetic Rubber
Hifza NasirHifza Nasir
March 8, 2024
Natural rubber, derived from the latex of rubber trees, offers elasticity and resistance to abrasion, while synthetic rubber, produced from petroleum byproducts, provides enhanced chemical and temperature resistance.
Imax 2D vs. 2DImax 2D vs. 2D
Shumaila SaeedShumaila Saeed
February 14, 2024
Imax 2D offers an immersive, large-scale cinematic experience with enhanced sound and image quality, whereas standard 2D provides a traditional flat-screen viewing without these enhancements.
Single User Operating System vs. Multi User Operating SystemSingle User Operating System vs. Multi User Operating System
Shumaila SaeedShumaila Saeed
January 24, 2024
A Single User Operating System supports one user at a time, whereas a Multi User Operating System allows multiple users to operate simultaneously.
Broadsheet vs. TabloidBroadsheet vs. Tabloid
Shumaila SaeedShumaila Saeed
November 2, 2024
Broadsheet is a large-format newspaper focusing on serious content; Tabloid is a smaller, sensational news-focused paper.
Inox vs. Stainless SteelInox vs. Stainless Steel
Shumaila SaeedShumaila Saeed
January 10, 2024
Inox is a synonym for stainless steel, used mainly in Europe, while stainless steel is a corrosion-resistant alloy containing chromium.
8085 Microprocessor vs. 8086 Microprocessor8085 Microprocessor vs. 8086 Microprocessor
Shumaila SaeedShumaila Saeed
February 1, 2024
The 8085 is an 8-bit microprocessor with a 16-bit address bus, while the 8086 is a 16-bit microprocessor with a 20-bit address bus, marking a significant advancement in processing capabilities.
.380 vs. .38 Special.380 vs. .38 Special
Shumaila SaeedShumaila Saeed
April 20, 2024
The .380 is a short-range pistol cartridge with less recoil, while the .38 Special is a longer, more powerful revolver cartridge suitable for diverse uses.
Cat6 vs. Cat6ACat6 vs. Cat6A
Shumaila SaeedShumaila Saeed
December 7, 2024
Cat6 cables support speeds up to 1Gbps over 100 meters, whereas Cat6A extends to 10Gbps over the same distance, offering enhanced performance and reliability.
Xmas vs. ChristmasXmas vs. Christmas
Shumaila SaeedShumaila Saeed
February 27, 2024
Xmas is an abbreviation of Christmas, often used for convenience, while Christmas refers to the traditional Christian holiday celebrating the birth of Jesus Christ.
Coke vs. PepsiCoke vs. Pepsi
Shumaila SaeedShumaila Saeed
January 12, 2024
Coke and Pepsi are iconic cola beverages with distinct flavors; Coke has a sharper, vanilla-tinged taste, while Pepsi is sweeter with a citrusy flavor.
Positivism vs. Post-PositivismPositivism vs. Post-Positivism
Shumaila SaeedShumaila Saeed
May 26, 2024
Positivism emphasizes observable, empirical evidence and the scientific method, while post-positivism recognizes the limitations of pure objectivity and incorporates subjective perspectives.
NM3 vs. M3NM3 vs. M3
Hifza NasirHifza Nasir
April 19, 2024
NM3 measures gas volume under Normal conditions (0°C and 1.01325 bar), while M3 measures volume under the conditions at which it is measured, without standard adjustment.
Roman Catholic vs. Irish CatholicRoman Catholic vs. Irish Catholic
Shumaila SaeedShumaila Saeed
February 4, 2024
Roman Catholic refers to the global Christian church led by the Pope in Rome, while Irish Catholic denotes Roman Catholics in Ireland, often with unique cultural and historical aspects.
Shriners vs. MasonsShriners vs. Masons
Shumaila SaeedShumaila Saeed
February 29, 2024
Shriners are a subgroup within Freemasonry known for charitable work, especially children's hospitals; Masons are members of the larger, older fraternity of Freemasonry with broader goals and activities.
Assess vs. AssesAssess vs. Asses
Dua FatimaDua Fatima
April 13, 2024
"Assess" means to evaluate or estimate the nature, ability, or quality of something. "Asses" is the plural of "ass," referring to multiple donkeys or used pejoratively for foolish people.
Candescent vs. IncandescentCandescent vs. Incandescent
Shumaila SaeedShumaila Saeed
September 22, 2024
Candescent refers to glowing with heat, while incandescent involves light produced by heat. Both indicate forms of luminescence, yet differ in context and use.
Megabyte vs. GigabyteMegabyte vs. Gigabyte
Shumaila SaeedShumaila Saeed
February 8, 2024
A Megabyte (MB) is a unit of digital information storage equal to 1,024 kilobytes, while a Gigabyte (GB) is equal to 1,024 megabytes.
TPU vs. PUTPU vs. PU
Shumaila SaeedShumaila Saeed
April 26, 2024
TPU is a type of thermoplastic elastomer with high elasticity and durability, while PU, or polyurethane, is versatile with varying hardness and used in multiple applications.

Featured Comparisons

New Comparisons