Search code examples
c++inheritancepolymorphismoverridingvirtual-functions

In a multi-level inheritance, does a grandchild require to implement a pure virtual method, if its parent has already implemented it?


class A {
  public:  virtual void start() = 0;
};

class B : public A {
  public: void start();
};

class Ba : public B {    
};

Do we need to redefine start() in Ba or the parent's B::start() would be enough?


Solution

  • Parent's start() is enough.

    You should though use the override keyword for B's reimplementation :

    class B : public A {
      public: void start() override;
    };
    

    It makes it clear that the method is an implementation of a virtual one and the compiler will enforce that it will always be the case, even with future changes on class A.