+
+ When using inheritance, any methods in the base class that can be overridden MUST be marked as virtual.
+ In deriving classes, when a virtual method is overridden, then only use the override keyword.
+ If a method should not be overridden further, then use the final keyword alone.
+
+
+
+ // Good:
+ class Base
+ {
+ public:
+ virtual void Print() const;
+ virtual void SetPrintSpeed( float speed );
+ };
+
+ class Derived : public Base
+ {
+ public:
+ void Print() const override;
+ void SetPrintSpeed( float speed ) final;
+ };
+
+
+ If a class should not be overridden then use the final keyword on the class itself.
+ This should also be done for a derived class that should not to be overridden further as well.
+ Overridden methods within that class can be marked as final or override.
+
+
+
+ class Derived final : public Base
+ {
+ public:
+ void Print() const override;
+ void SetPrintSpeed( float speed ) final;
+ };
+
+
Overloading
+
+ Overloading of Base class methods SHOULD be avoided but if it's required, then use the using keyword.
+
+
+
+ class Derived : public Base
+ {
+ public:
+ void Print( float number ) const; // overloaded member
+ using Base::Print; // Make the Base class' Print method visible here as well.
+ };
+
+
+ If we do not add the using line, then we can only use the overloaded Print method for a Derived object (unless we cast to the Base class). + Attempting to use the base class' Print() method on a Derived object would result in a compilation error. +
++
+