1

I've been playing around with inheritance and I've tried this code:

#include <iostream> 
#include <string>

class Foo
{
public:
    virtual void func() = 0;

protected:
    virtual void doSum() const = 0;
};

class Bar : public Foo
{
public:
    void func() { 
        doSum();
    }

protected:
    void doSum() const
    {
        std::cout << "hi, i'm doing something" << std::endl;
    }
};

int main() 
{ 
    Foo* ptr = new Bar();
    ptr->func();

   return 0; 
}

So I've also tried replacing the protected keyword in the class Bar with private like this :

private:
    void doSum() const
    {
        std::cout << "hi, i'm doing something" << std::endl;
    }

and the code happened to work just the same...

So my question is, is there any difference if I declare a protected method private when implementing a derived class? If so, what are they? Am I even allowed to do this?

grybouilli
  • 185
  • 2
  • 11

2 Answers2

2

So my question is, is there any difference if I declare a protected method private when implementing a derived class?

Yes.

If so, what are they?

That will prevent the next level of derived class from being able to call the derived class's implementation.

class Foo
{
   protected:
      virtual void doSum() const = 0;
};

class Bar : public Foo
{
   private:
      void doSum() const
      {
         std::cout << "hi, i'm doing something" << std::endl;
      }
};

class Baz : public Bar
{
   public:
      void doSum() const
      {
         //===========================
         Bar::doSum(); // NOT ALLOWED
         //===========================
      }
};

Am I even allowed to do this?

Yes.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Sure. but the `fooPtr->doSum()` would still end up calling the `Baz` implementation. – cplusplusrat Apr 08 '19 at 16:07
  • Thanks for the detailed answer! So, if i'm not intending to derive Bar, should I rather let it be protected or make it private? Or there's literally no relevance? – grybouilli Apr 08 '19 at 16:07
  • @Grybouilli, if you are absolutely certain, then yes, then it is not relevant. – R Sahu Apr 08 '19 at 16:08
  • 1
    @Grybouilli "So, if i'm not intending to derive Bar, should I rather let it be protected or make it private?" - If you do not want it to be possible to derive from `Bar`, you should make the class `final`. You can also make individual functions `final` to prevent them from being overridden while still letting others derive from `Bar`. – Jesper Juhl Apr 08 '19 at 16:12
1

So my question is, is there any difference if I declare a protected method private when implementing a derived class?

No. There is no difference. Unfortunately, C++ standard does not impose any requirement on the derived class to place the overriding virtual function within any particular accessibility scope. This means, the base class could declare a virtual method protected, and the derived class could implement the method in public/protected/private scope and the code will still be legal and will work.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
cplusplusrat
  • 1,435
  • 12
  • 27