Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C++ final specifier

  • Difficulty Level : Easy
  • Last Updated : 03 Nov, 2020

In Java, we can use final for a function to make sure that it cannot be overridden. We can also use final in Java to make sure that a class cannot be inherited. Similarly, the latest C++ standard C++ 11 added final. 
Use of final specifier in C++ 11: 
Sometimes you don’t want to allow derived class to override the base class’ virtual function. C++ 11 allows built-in facility to prevent overriding of virtual function using final specifier. 

Consider the following example which shows use of final specifier. This program fails in compilation. 

Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.

CPP




#include <iostream>
using namespace std;
 
class Base
{
public:
    virtual void myfun() final
    {
        cout << "myfun() in Base";
    }
};
class Derived : public Base
{
    void myfun()
    {
        cout << "myfun() in Derived\n";
    }
};
 
int main()
{
    Derived d;
    Base &b = d;
    b.myfun();
    return 0;
}

Output: 

prog.cpp:14:10: error: virtual function ‘virtual void Derived::myfun()’
     void myfun()
          ^
prog.cpp:7:18: error: overriding final function ‘virtual void Base::myfun()’
     virtual void myfun() final 

2nd use of final specifier: 
final specifier in C++ 11 can also be used to prevent inheritance of class / struct. If a class or struct is marked as final then it becomes non inheritable and it cannot be used as base class/struct. 



The following program shows use of final specifier to make class non inheritable: 

CPP




#include <iostream>
class Base final
{
};
 
class Derived : public Base
{
};
 
int main()
{
    Derived d;
    return 0;
}

Output: 

error: cannot derive from ‘final’ base ‘Base’ in derived type ‘Derived’
 class Derived : public Base

final in C++ 11 vs in Java 
Note that use of final specifier in C++ 11 is same as in Java but Java uses final before the class name while final specifier is used after the class name in C++ 11. Same way Java uses final keyword in the beginning of method definition (Before the return type of method) but C++ 11 uses final specifier after the function name.
 

CPP




class Test
{
    final void fun()// use of final in Java
    { }
}
class Test
{
public:
    virtual void fun() final //use of final in C++ 11
    {}
};

Unlike Java, final is not a keyword in C++ 11. final has meaning only when used in above contexts, otherwise it’s just an identifier. 
One possible reason to not make final a keyword is to ensure backward compatibility. There may exist production codes which use final for other purposes. For example the following program compiles and runs without error. 
 

CPP




#include <iostream>
using namespace std;
 
int main()
{
    int final = 20;
    cout << final;
    return 0;
}

Output: 

20

In java, final can also be used with variables to make sure that a value can only be assigned once. this use of final is not there in C++ 11.
This article is contributed Meet Pravasi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!