Cast all your worries on him, because he cares for you. 1 Peter 5: 7 (Bible)
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.
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:
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.
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.
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
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Using a variable as format specifier in C
- _Noreturn function specifier in C
- Understanding constexpr specifier in C++
- Difference between %d and %i format specifier in C language
- Simulating final class in C++
- Difference between Sentinel and Counter Controlled Loop in C
- Why do we need reference variables if we have pointers
- What are the differences between C and Embedded C?
- Similarities and Difference between Java and C++
- Macros and its types in C/C++
- How to find index of a given element in a Vector in C++
- Introduction to Complex Objects and Composition
- Draw a smiley face using Graphics in C language
- sort() vs. partial_sort() vs. nth_element() + sort() in C++ STL