Example & Tutorial understanding programming in easy ways.

Define final in C++

Final in C++:
-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.

program:

#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 Derivedn";
}
};
int main()
{
Derived d;
Base &b = d;
b.myfun();
return 0;
}


output-

Give some error




Read More →