There’s a good description of what override does here: override specifier (since C++11) - cppreference.com
Also for virtual: virtual function specifier - cppreference.com
To summarise, override indicates that the method is overriding a base class method and that method is declared as virtual. It is optional BUT if the signatures for the overriding method doesn’t match, it can introduce a bug in your code and adding override can highlight the bug during compile.
Edit: some sample code below. Removing override
works - the calls to hello both print out hello and hello world. if you take virtual
out, and leave override
, compile fails. If you take out both virtual
and override
, the program still works but b.Hello()
doesn’t print Hello World like it does when virtual
is present. I’ve attached the screenshot of the results of this test.
#include <iostream>
class BaseTest
{
public:
virtual void Hello();
};
class InheritTest : public BaseTest
{
public:
void Hello() override;
};
void BaseTest::Hello()
{
std::cout << "Hello!\n";
}
void InheritTest::Hello()
{
BaseTest::Hello();
std::cout << "Hello World!\n";
}
int main()
{
std::cout << "Inheritance experiments\n";
InheritTest c;
BaseTest& b =c;
c.Hello();
b.Hello();
}