~/okanakyuz
← all posts

Virtual Methods and Abstract Classes in C++

Virtual methods, pure virtual methods and abstract classes in C++, plus override, final and why a base class needs a virtual destructor.

7 March 2020updated 25 November 20261 min read

Diagram of a virtual pointer pointing to a virtual table

virtual is a keyword to declare that a method can be overridden by a child class. A virtual method doesn’t have to be overridden, and the class is not abstract just because it has virtual methods.

If a virtual method is declared with = 0, it is a pure virtual method. A class with at least one pure virtual method is abstract: you can’t create an object of it, and a child class must override that method to be instantiable. If all methods are pure virtual, the class works like an interface.

The override keyword shows that the method overrides a virtual method of the base class. It is optional, but you should use it, because the compiler gives an error if there is nothing to override (for example, when the signature has a typo).

A smart pointer is the modern way to hold a pointer to a polymorphic object. Below I used std::unique_ptr; std::shared_ptr works too.

#include <iostream>
#include <memory>

class IParent{
public:
    // vmethod is pure virtual (= 0)
    // as a result
    // this class is abstract.
    virtual void vmethod() = 0;
    // we delete children through IParent*,
    // so the destructor must be virtual
    virtual ~IParent() = default;
};

class MyChild : public IParent{
public:
    // all pure virtual methods must be overridden,
    // mark them with the override keyword
    void vmethod() override{
        std::cout << "vmethod()" << std::endl;
    }
};

int main()
{
    // IParent p; // error: IParent is abstract
    std::unique_ptr<IParent> p = std::make_unique<MyChild>();
    p->vmethod();
    return 0;
}

The destructor of a base class should be virtual every time you delete a child object through a base class pointer. Otherwise, deleting it is undefined behavior; in practice the child’s destructor is not called.

#include <iostream>
#include <memory>

class MyParent{
public:
    MyParent(){
        std::cout << "MyParent()" << std::endl;
    }
    virtual ~MyParent(){
        std::cout << "~MyParent()" << std::endl;
    }
};

class MyChild : public MyParent{
public:
    MyChild(){
        std::cout << "MyChild()" << std::endl;
    }
    ~MyChild() override{
        std::cout << "~MyChild()" << std::endl;
    }
};

int main()
{
    // prints ~MyChild() and then ~MyParent()
    std::unique_ptr<MyParent> p = std::make_unique<MyChild>();

    return 0;
}

You can not override an inherited method if it is not declared virtual. You can write a method with the same name in the child class, but it only hides the base version, and a call through a base pointer still goes to the base method.

If a class is not suitable for inheriting, we should mark it with the final keyword. You can also put final on a single virtual method to stop further overriding.

#include <iostream>

class MyClass final{
public:
    MyClass(){
        std::cout << "MyClass()" << std::endl;
    }
    // not virtual, and nobody can inherit
    // from MyClass anyway
    void method();
    // a non-virtual destructor is fine here,
    // because the class is final
    ~MyClass(){
        std::cout << "~MyClass()" << std::endl;
    }
};

// class Child : public MyClass {}; // error: MyClass is final