Home >Backend Development >C++ >What's the Difference Between Virtual and Pure Virtual Functions in Object-Oriented Programming?
Understanding Virtual and Pure Virtual Functions
Virtual functions play a crucial role in object-oriented programming by enabling runtime polymorphism. As defined by Wikipedia, a virtual function is an overridable method or function that allows dynamic dispatch. This means that the specific function to be executed is determined based on the type of object at runtime, rather than at compile time.
In contrast to non-virtual functions, when a virtual function is overridden in a derived class, the version defined in the most-derived class is used throughout the class hierarchy. This ensures that the most specific implementation of the function is always utilized.
Pure virtual functions, also known as pure virtual methods, take this concept further. They are virtual functions that must be implemented in a derived class if that class is not abstract. As such, a pure virtual function effectively mandates the presence of a particular behavior in derived classes.
Abstract classes are those that contain one or more pure virtual functions. Such classes cannot be instantiated directly; instead, a derived class providing implementations for all pure virtual functions must be used.
To illustrate the differences between virtual and pure virtual functions, consider the following C code:
Base.hpp:
struct Base { virtual ~Base() = default; virtual void foo() { std::cout << "Base::Foo" << std::endl; } };
Derived.hpp:
struct Derived : public Base { void foo() override { std::cout << "Derived::Foo" << std::endl; } };
main.cpp:
int main() { Base base; base.foo(); // Calls Base::Foo Derived derived; derived.foo(); // Calls Derived::Foo Base* derived_as_base = &derived; derived_as_base->foo(); // Calls Derived::Foo (due to dynamic dispatch) }
In this code, the Base class contains a virtual function foo that prints "Base::Foo." The Derived class overrides this function to print "Derived::Foo." When a Base object calls foo, the output is "Base::Foo." However, when a Derived object (accessed as a Base pointer) calls foo, the output is "Derived::Foo" due to dynamic dispatch.
Now, let's consider a pure virtual function:
PureBase.hpp:
struct PureBase { virtual ~PureBase() = default; virtual void foo() = 0; // Pure virtual function };
PureDerived.hpp:
struct PureDerived : public PureBase { void foo() override { std::cout << "PureDerived::Foo" << std::endl; } };
In this code, the PureBase class contains a pure virtual function foo that is not defined in the base class. As a result, the PureBase class is abstract because it lacks a complete implementation.
PureMain.cpp:
int main() { // error: cannot declare variable 'pureBase' to be of abstract type 'PureBase' // PureBase pureBase; // pureBase.foo(); PureDerived pureDerived; pureDerived.foo(); // Calls PureDerived::Foo }
In this example, attempting to instantiate a PureBase object directly results in a compiler error. However, creating a PureDerived object is allowed because it provides an implementation for the foo function.
To summarize, virtual functions provide dynamic dispatch and can be overridden in derived classes. Pure virtual functions, on the other hand, are required to be implemented in derived classes and enforce specific behaviors. Together, these concepts play a vital role in enabling polymorphic functionality in object-oriented programming.
The above is the detailed content of What's the Difference Between Virtual and Pure Virtual Functions in Object-Oriented Programming?. For more information, please follow other related articles on the PHP Chinese website!