C++ 11 final specifier how to use


In this article, we are going to learn C++ 11 final specifier how to use, which was introduced in C++ 11. The final specifier is used with the function which is declared as virtual in C++ classes.

What is C++ 11 final specifier

Virtual functions are a robust feature of the C++ language and they are used to achieve the run time Polymorphism. Virtual functions play an important role when we want to achieve different functionalities with the same functions in Base and derived classes. We are already aware of the syntax that we use to declare Virtual functions, where we add the virtual keyword in the function declaration.

With the introduction of C++ 11, we can use the final keyword to tell the compiler that this is the final definition of this function and this function can not be overridden in the derived classes further. This specifier when added to a virtual function Specifies that a virtual function cannot be: overridden in a derived class. Also when the final specifier is added to the class declaration then that class cannot be inherited in other classes. That class becomes the final class in that inheritance hierarchy.

so let us understand these concepts with some examples and in detail.

1. Can not Override a Final function

We are taking the example of class inheritance to understand the Final specifier in C++

C++ Program Example

class sampleClassA {
  virtual void sum();
  virtual int Muliply();

We have another class that is inheriting sampleClassA

class sampleClassB : sampleClassA
  void sum() final; 
  void Muliply() final; 

Let us take one more derived class which is derived from class sampleClassB.We will try to use the final and override keywords in this class for the functions which we already declared in sampleClassB with the final keyword. so let’s see how this new class will react to this.

As you can see below the code will flag an error when you are trying to declare the sum() function. This function has already been marked as final in sampleClassB. This is the reason it can not be overridden in this derived class.

class sampleClassC : sampleClassB
  void sum() final;        // error -- declaration of 'sum' overrides a 'final' function
   void Muliply() override;  // error -- 

2. class cannot be inherited from

Inheritance is one of the powerful features of c++. We can get desired results by using the final keyword in the class
inheritance implementation.

For some of our design purposes, we sometimes do not want any other class to derive our class. So to achieve that
we can make use of the final keyword, which helps us in restricting the inheritance of a class. so let us understand this with the below example:

We are going to take a class sampleClassA and we are going to mark this class as final in the declaration itself. This final keyword will make this sample class not inheritable by any of the classes.

C++ Program Example

class sampleClassA final {
   void sum();
   int Muliply();

Now if we try to inherit sampleClassA in sampleClassB the compiler will throw an error to us and will not let us allow to
inherit the final class.

class sampleClassB : sampleClassA  // error -- base 'sampleClassA' is marked 'final'

This is also one of the example of final keyword. The use of Final keyword can be done very efficiently in our design where we want to put some restrictions on our class or a function of a class.

I hope you could understand the use of the Final keyword and also the use cases where we can make use of the final keyword to help us design a module.

Happy Learning!!