In the example below (taken from inheritance note), we have used public specifier to inherit from base class:

class Base
{
    int m_base{};
 
public:
    Base(int value = 5)
        : m_base{value}
    {
        std::cout << "Base constructor with value " << m_base << '\n';
    }
};
 
class Derived : public Base
{
    int m_derived{};
public:
    Derived(int value = 10)
        : m_derived{value}
    {
        std::cout << "Derived constructor with value " << m_derived << '\n';
    }
};

Here, this public is called an access specifier. Access specifier in the context of inheritance specify how the member of the base class would be inherited to the derived class.

Inheritance using public

This is most commonly used type of inheritance where data members are publicly inherited from the base class.

When base class is inherited with public specifier, base class’s member which are

  • private are inaccessible in the derived class.
  • protected are inherited are protected members.
  • and public are inherited as public members.

So, if class Base has three data members of each type:

  1. m_base_private => not accessible in Derived class
  2. m_base_protected => accessible within the derived class but not to the outside world.
  3. m_base_public => accessible within the derived class and to the outside world.

Inheritance using protected

This one is rarely used.

When base class is inherited with protected specifier, base class’s member which are

  • private are inaccessible in the derived class.
  • protected are inherited are protected members.
  • and public are inherited as protected members.

So, if class Base has three data members of each type:

  1. m_base_private => not accessible in Derived class
  2. m_base_protected => accessible within the derived class but not to the outside world.
  3. m_base_public => accessible within the derived class but not to the outside world.

Inheritance using private

This one is also rarely used.

When base class is inherited with private specifier, base class’s member which are

  • private are inaccessible in the derived class.
  • protected are inherited are private members.
  • and public are inherited as private members.

So, if class Base has three data members of each type:

  1. m_base_private => not accessible in Derived class
  2. m_base_protected => accessible within the derived class but not to the outside world.
  3. m_base_public => accessible within the derived class and to the outside world.

Final table

Base class memberpublic inheritanceprotected inheritanceprivate inheritance
publicpublicprotectedprivate
protectedprotectedprotectedprivate
privateNot directly accessible in the derived classNot directly accessible in the derived classNot directly accessible in the derived class

References

  1. https://www.learncpp.com/cpp-tutorial/inheritance-and-access-specifiers/