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

Poem vs. PoetryPoem vs. Poetry
Shumaila SaeedShumaila Saeed
December 25, 2023
A poem is a piece of writing that expresses ideas and emotions with a distinctive style and rhythm; poetry is the art form of writing such pieces.
LTE vs. CDMALTE vs. CDMA
Shumaila SaeedShumaila Saeed
February 4, 2024
LTE (Long Term Evolution) is a 4G wireless communication standard with high-speed data transfer, while CDMA (Code Division Multiple Access) is an older 2G/3G technology for mobile networks.
Smart TV vs. Android TVSmart TV vs. Android TV
Shumaila SaeedShumaila Saeed
December 25, 2023
A Smart TV is an internet-connected television with a variety of apps, while an Android TV is specifically a Smart TV powered by Google's Android TV operating system.
White Collar Crime vs. Blue Collar CrimeWhite Collar Crime vs. Blue Collar Crime
Shumaila SaeedShumaila Saeed
December 25, 2023
White Collar Crime involves non-violent, financially motivated offenses often committed by professionals, while Blue Collar Crime refers to physical or violent crimes often by manual laborers.
Japanese Eyes vs. Chinese EyesJapanese Eyes vs. Chinese Eyes
Shumaila SaeedShumaila Saeed
December 25, 2023
Japanese Eyes and Chinese Eyes refer to linguistic structures in Japanese and Chinese respectively, each reflecting unique aspects of grammar and syntax.
Seagate Exos x16 vs. Seagate Exos x18Seagate Exos x16 vs. Seagate Exos x18
Shumaila SaeedShumaila Saeed
February 8, 2024
The Seagate Exos X16 offers up to 16TB storage with a focus on high-capacity data centers, while the Exos X18 upgrades to 18TB, enhancing performance and capacity for enterprise demands.
Social Change vs. Cultural ChangeSocial Change vs. Cultural Change
Shumaila SaeedShumaila Saeed
December 25, 2023
Social change refers to shifts in societal structures and institutions, impacting behaviors and relationships among people. Cultural change pertains to alterations in a group's shared beliefs, values, and customs, influencing their way of life.
NAT vs. PATNAT vs. PAT
Shumaila SaeedShumaila Saeed
March 5, 2024
NAT (Network Address Translation) translates private IP addresses to a public one for internet access. PAT (Port Address Translation) maps multiple private IP addresses to a single public IP using different ports.
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.
Assemble vs. BuildAssemble vs. Build
Shumaila SaeedShumaila Saeed
December 25, 2023
Assemble refers to the act of gathering and organizing pre-existing components, while build involves the creation of something new by combining various materials or elements.
2 Pole Motors vs. 4 Pole Motors2 Pole Motors vs. 4 Pole Motors
Shumaila SaeedShumaila Saeed
December 25, 2023
2 Pole Motors have one pair of magnetic poles and run at higher speeds, while 4 Pole Motors have two pairs of poles and operate at lower speeds, offering higher torque.
Oscar vs. EmmyOscar vs. Emmy
Shumaila SaeedShumaila Saeed
February 20, 2024
The Oscar is an award for cinematic achievements, while the Emmy recognizes excellence in television.
Hard Copy vs. Soft CopyHard Copy vs. Soft Copy
Shumaila SaeedShumaila Saeed
December 25, 2023
A Hard Copy is a physical version of a document or file, usually on paper, while a Soft Copy is a digital version of the document, stored electronically.
Payment vs. RemittancePayment vs. Remittance
Dua FatimaDua Fatima
April 9, 2024
Payment is a transfer of money for goods or services, while remittance involves sending money to a distant location, often overseas.
Gorilla Glass 3 vs. Gorilla Glass 5Gorilla Glass 3 vs. Gorilla Glass 5
Shumaila SaeedShumaila Saeed
January 1, 2024
Gorilla Glass 3 offers improved scratch resistance and durability compared to its predecessors, while Gorilla Glass 5 focuses on enhanced drop protection and toughness.
Gorilla Glass vs. Panda GlassGorilla Glass vs. Panda Glass
Shumaila SaeedShumaila Saeed
January 5, 2024
Gorilla Glass is a highly durable, scratch-resistant glass used in electronic devices, while Panda Glass is a similar protective glass known for its high transparency and toughness.
ISO 9000 vs. ISO 14000ISO 9000 vs. ISO 14000
Shumaila SaeedShumaila Saeed
February 13, 2024
ISO 9000 focuses on quality management and customer satisfaction, whereas ISO 14000 concentrates on environmental management and reducing environmental impact.
Analog Computer vs. Digital ComputerAnalog Computer vs. Digital Computer
Shumaila SaeedShumaila Saeed
December 25, 2023
An Analog Computer processes continuous data, whereas a Digital Computer processes data in discrete numerical form.
Nike Air Force 1 LE vs. Nike Air Force 1 '07Nike Air Force 1 LE vs. Nike Air Force 1 ’07
Hifza NasirHifza Nasir
April 16, 2024
Nike Air Force 1 LE often represents limited edition releases with unique designs, while Nike Air Force 1 '07 is a modern version of the classic, maintaining the iconic style with updated materials.
Grand Opening vs. Soft OpeningGrand Opening vs. Soft Opening
Shumaila SaeedShumaila Saeed
December 25, 2023
A Grand Opening is a highly publicized and celebratory launch of a business or venue, while a Soft Opening is a more subdued trial opening, often with limited services or a smaller audience.
Federalists vs. Democratic RepublicansFederalists vs. Democratic Republicans
Shumaila SaeedShumaila Saeed
March 24, 2024
Federalists favored strong central government and commercial economy, while Democratic Republicans advocated states' rights and agrarianism.
Catapult vs. TrebuchetCatapult vs. Trebuchet
Shumaila SaeedShumaila Saeed
January 4, 2024
A catapult is a ballistic device using tension or torsion to launch projectiles, while a trebuchet is a type of catapult using a counterweight for greater force and distance.
Ginger vs. RedheadGinger vs. Redhead
Shumaila SaeedShumaila Saeed
February 2, 2024
"Ginger" often connotes a fiery red hair color and a pale complexion, while "redhead" is a more general term for anyone with red hair, regardless of shade or skin tone.
Plant Cell vs. Animal CellPlant Cell vs. Animal Cell
Shumaila SaeedShumaila Saeed
December 25, 2023
Plant cells have a cell wall and chloroplasts for photosynthesis, while animal cells lack these but have centrioles.

Featured Comparisons

New Comparisons