In the last weeks something is bugging my brain about virtual
and override
.
I've learned that when you do inheritance with virtual function you have to add virtual
to let the compiler know to search for the right function.
Afterwards I learned also that in c++ 11 there is a new keyword - override
. Now I'm a little confused; Do i need to use both virtual and override keywords in my program, or it's better to use only one of them?
To explain myself - code examples of what I mean:
class Base
{
public:
virtual void print() const = 0;
virtual void printthat() const = 0;
virtual void printit() const = 0;
};
class inhert : public Base
{
public:
// only virtual keyword for overriding.
virtual void print() const {}
// only override keyword for overriding.
void printthat() const override {}
// using both virtual and override keywords for overriding.
virtual void printit() const override {}
};
What is the best method?