The final specifier was introduced in C++11 with two main purposes −
- The first purpose is to prevent inheritance. When a class is marked with final, other classes can not inherit from it.
- The second purpose it to avoid function overriding. When you mark a virtual function with final, then it can not be overridden in the derived classes.
Read this chapter to learn how to use the final specifier in C++.
Using the final Specifier with Class
You can mark a class as final to prevent inheritance. After marking a class as final, you can no longer use this class for inheritance. Other classes will not be able to inherit them.
The syntax for using final specifier with a class is given below −
classMyClassfinal{// class members};
Example
Here is an example demonstrating the difference between a class when used with final and when used without final −
Class with final Class without final
In this example, we are using the final specifier with Base class and the Derived class is inheriting the base class. It will give an error in output as shown below −
#include <iostream>usingnamespace std;classBasefinal// final class{public:voiddisplay(){
cout <<"Base class"<< endl;}};// Inheriting from final classclassDerived:public Base{public:voidshow(){
cout <<"Derived class"<< endl;}};intmain(){
Base b;
b.display();return0;}
The output of the above code is given below −
main.cpp:14:7: error: cannot derive from 'final' base 'Base' in derived
type 'Derived'
14 | class Derived : public Base
| ^~~~~~~
Using the final Specifier with Function
You can mark a function with final to avoid overriding the function. After marking a function as final, you can no longer override that function. The function can only be made final if the function is a virtual function.
The syntax to use final specifier with a function is given below −
classBase{public:virtualvoiddisplay()final{// implementation}};
Example
The example below demonstrates the difference between a function when used with final and when used without final −
Function with final Function without final
In this example, we have used final specifier on the virtual function breathe() in Mammal class. In the Dog class, we are trying to override the breathe() function which will give an error in the output.
#include <iostream>usingnamespace std;classAnimal{public:virtualvoidmakeSound(){
cout <<"Animal sound"<< endl;}virtualvoidbreathe(){
cout <<"Animal breathing"<< endl;}};classMammal:public Animal{public:voidmakeSound()override{
cout <<"Mammal sound"<< endl;}// final functionvoidbreathe()finaloverride{
cout <<"Mammal breathing"<< endl;}};classDog:public Mammal{public:voidmakeSound()override{
cout <<"Bark"<< endl;}// This would cause an errorvoidbreathe()override{
cout <<"Dog breathing"<< endl;}};intmain(){
Dog dog;
dog.makeSound();
dog.breathe();return0;}
The output of the above code is as follows −
main.cpp:42:10: error: virtual function 'virtual void Dog::breathe()' overriding
final function
42 | void breathe() override
| ^~~~~~~
main.cpp:27:10: note: overridden function is 'virtual void Mammal::breathe()'
27 | void breathe() final override
| ^~~~~~~
Conclusion
The final specifier in C++ helps in preventing the inheritance of classes and the overriding of the virtual function. In this chapter, we explained in detail how the final specifier works with class and virtual function.