Search code examples
c++classderived-classpure-virtual

Pure virtual method must be implemented only in immediate derived classes - why?


Suppose there is an abstract class A and two classes B & C derived from A and B respectively

class A
{
    virtual void print() = 0;
};

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

//print() must be implemented
void B::print()
{
    std::cout << "Hello" << std::endl;
}

class C : public B
{
    //print() does not require to be implemented
};

I understand that from C's perspective of its immediate parent B, print() is not a pure virtual method, but from C's perspective of its grandparent A, print() is a pure virtual method. So, shouldn't it require implementation in C too?


Solution

  • C doesn't "care" how it gets an implementation, just that it gets one. And it gets one from B. However B receives no implementation of print and therefore needs to implement print on its own

    EDIT: Per comments below, the implementation must not be marked as pure virtual again