Override specifier
You can use the Override keyword to specify a member function that overrides a virtual function in a base class.
Grammar
Function-declaration Override;
Note
Override is context-sensitive and has special meaning only when it is used after a member function declaration, otherwise it is not a reserved keyword.
Using override helps to prevent unexpected inheritance behavior from occurring in your code. The following example demonstrates that you might not intend to use the member function behavior of a derived class without using override. The compiler does not emit any errors for this code.
Class BaseClass
{
virtual void Funca ();
virtual void FUNCB () const;
virtual void funcc (int = 0);
void Funcd ();
Class Derivedclass:public BaseClass
{
virtual void Funca ();/OK, works as intended
virtual void funcb (); /DERIVEDCLASS::FUNCB () is non-const, so it does not
//override BASECLASS::FUNCB () const and it are new member Func tion
virtual void funcc (double = 0.0);//DERIVEDCLASS::FUNCC (double) has a different
//parameter type than Bas ECLASS::FUNCC (int), so
/DERIVEDCLASS::FUNCC (double) are a new member function
};
When using override, the compiler generates an error without creating a new member function without prompting.
Class BaseClass
{
virtual void Funca ();
virtual void FUNCB () const;
virtual void funcc (int = 0);
void Funcd ();
Class Derivedclass:public BaseClass
{
virtual void Funca () override;//OK
virtual void funcb () override;// Compiler ERROR:DERIVEDCLASS::FUNCB () does not
//override BASECLASS::FUNCB () const
virtual void FUNCC (double = 0.0) override; Compiler error:
//DERIVEDCLASS::FUNCC (double) does not
//override BASECLASS::FUNCC (int)
void FUNCD () override; Compiler ERROR:DERIVEDCLASS::FUNCD () does not
//override the Non-virtual Baseclass::funcd ()
};
To specify that a function cannot be overridden and cannot inherit a class, use the final keyword.
Final Descriptor
You can use the final keyword to specify a virtual function that cannot be overridden in a derived class. You can also use it to specify a class that cannot be inherited.
Grammar
Function-declaration final;
Class Class-name Final base-classes
Note
Final is context-sensitive and has special meaning only if it is used after a function declaration or a class name, otherwise it is not a reserved keyword.
When final is used in a class declaration, base-classes is an optional part of the declaration.
The following example uses the final keyword to specify that a virtual function cannot be overridden.
Class BaseClass
{
virtual void func () final;
Class Derivedclass:public BaseClass
{
virtual void func ();//compiler error:attempting to
//override a fi nal function
};
For information about how to specify that you can override member functions, see the override specifier.
The next example uses the final keyword to specify that a class cannot be inherited.
Class BaseClass final
{
};
Class Derivedclass:public BaseClass//Compiler Error:baseclass is
//marked as non-inheritable
{
};