C++11: Override and Final keyword

The explicit override and final keywords in C++11 (Introduction to C++11) enhances the polymorphism capabilities of C++. These new features provide extra control over virtual functions to the programmer. In this post, we will discuss about these new features and their usage.

Override

The explicit override keyword overrides the marked virtual function in derived class as override version of base class virtual functions. This ensures that the overrides are done by the programmer intentionally which reduces the chances of unwanted mistakes by providing clarity and hence improves the code readability and maintainability.

Syntax

#include <iostream>
class Base {
public:
    virtual void print() const;
};
class Derived : public Base {
public:
    void print() const override;  // Marked override explicitly
};

In the above example, “print” function in derived class are defined with “override” keyword to indicate that the override is made intentional by the programmer and thus improves the code readability.

Advantages

  • Readability – Explicit override clearly indicates which functions are overridden intentionally and hence improves the code readability.
  • Catching unwanted errors – Explicit override keyword helps in catching the unwanted errors in compile time only. Override keyword will throw compile time error if derived class function signature does not match with Base class. This avoids unintentional mistake sometimes done by the programmer.
  • Easy code maintenance – Explicit override keyword helps the code maintainability also. In case any changes done in Base class functions then compile time error will trigger programmer to update all the derive classes accordingly.

Final

The “final” keyword introduced in c++11 (Introduction to C++11), allows programmers to stop further overriding of virtual functions. If a class or function is marked as “final”, then it can’t be overridden by any derived class and that version will become the final version of that class or function.

Syntax

#include <iostream>
class BaseFinal {
public:
    virtual void cantOverride() const final;
};
class DerivedFinal : public BaseFinal {
public:
    void cantOverride() const;  // Error: Attempting to override final function
};

In the above example, “cantOverride” function in BaseFinal class is marked as final indicating compiler to not allow any overriding of this function. Hence, a compiler error will be thrown in case DerivedFinal tries to override it.

Advantages

  • Preventing unnecessary overrides — The “final” keyword helps programmer to prevent unnecessary overrides of the virtual functions. This keyword allows programmer to ensure that corresponding function version remains intact as intended by the programmer.
  • Clearer design – The “final” keyword allows the programmer to mark a class as base implementation. This stops the unnecessary overriding and ensuring that the decision taken at the designing phase is followed.
  • Optimized Performance – The “final” keyword indicated compiler that further overriding of the function is not allowed. Thus, compiler can apply various optimizations to improve runtime performance of the system.

Leave a Reply

Your email address will not be published. Required fields are marked *