In this blog post we will talk about three c++ 11 features final, override and lamda expression. Let us discuss c++ 11 final specifier with example.

c++ final specifier

  • It is to prevent a class from being inherited. So below code will not compile. it will error out “error: cannot derive from ‘final’ base ‘Base’ in derived type ‘Derived’.

  • It is also used to mark a virtual function so as to prevent it from being overridden in the derived classes.

  • Only virtual functions and class can be marked final. Non virtual functions can not be marked final.

c++ 11 override keyword

override in c++ 11 is not a keyword. It is a identifier. from wikipedia “The override special identifier means that the compiler will check the base class(es) to see if there is a virtual function with this exact signature. And if there is not, the compiler will error out”.

override in C++11 is nothing more than a check to make sure that the function being implemented is the overrideing of a virtual function in the base class.

What is benefit of using override?

Let us see below example.

The above code will compile successfully. But there is a issue if a developer has declared virtual method foo(int a) in base class with the intention of overriding it in derived class. We can see in derived class the signature of method foo is quite different compare to its signature in Base class.

Let us specify overriding method foo in Derived class with keyword ‘override’ and try to compile.

compile time error:

error: ‘void Derived::foo(double)’ marked override, but does not override

So using override key word, there would be more safety to our program.

Lambda expression in C++11 and C++14

Read This link http://wikistack.com/lambda-expression-in-c11-and-c14/

c++ 11 tutorial  final, override and lamda expression