Home > Backend Development > C++ > Virtual vs. Pure Virtual Functions: What's the Difference?

Virtual vs. Pure Virtual Functions: What's the Difference?

Mary-Kate Olsen
Release: 2024-12-29 21:36:16
Original
344 people have browsed it

Virtual vs. Pure Virtual Functions: What's the Difference?

Virtual Functions Explained: What They Are and How They Differ from Pure Virtual Functions

In object-oriented programming, the concept of virtual functions is integral to runtime polymorphism. A virtual function is one that can be overridden in derived classes to provide different implementations at different levels of the class hierarchy.

The key distinction between a virtual function and a non-virtual function is that the most-derived version is used at all levels of the class hierarchy when a virtual function is overridden. In contrast, a non-virtual function's functionality remains unchanged in the base class, even if it is overridden in a derived class.

Here's an example to illustrate the behavior of virtual functions:

struct Base {
    virtual ~Base() = default;

    virtual void foo() {
        std::cout << "Base::Foo" << std::endl;
    }
};

struct Derived final: Base {
    void foo() override {
        std::cout << "Derived::Foo" << std::endl;
    }
};
Copy after login

Running this code produces the following output:

Base::Foo
Derived::Foo
Derived::Foo
Copy after login

As you can see, when the foo() function is called on an instance of the Derived class, the Derived::foo() implementation is used, even when accessing the instance through a base class pointer (Base* derived_as_base).

Pure Virtual Functions: A Special Case

A pure virtual function is a virtual function that is required to be implemented in derived classes. It is not defined in the base class and must be overridden in a derived class to make the class concrete. A class with one or more pure virtual functions is considered abstract and cannot be instantiated directly.

Here's an example of a pure virtual function:

struct Base {
    virtual ~Base() = default;

    virtual void foo() = 0;
};

struct Derived final: Base {
    void foo() override {
        std::cout << "Derived::Foo" << std::endl;
    }
};
Copy after login

In this case, the class Base is abstract because it contains a pure virtual function foo(). To use this class, a derived class must implement the foo() function, making the derived class a concrete class that can be instantiated.

In summary, virtual functions allow you to override base class functionality at different levels of the hierarchy, while pure virtual functions require derived classes to implement specific functionality to make the class concrete. Understanding these concepts is crucial for writing extensible and polymorphic code in object-oriented programming.

The above is the detailed content of Virtual vs. Pure Virtual Functions: What's the Difference?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template