Understanding Access Control in C++ Inheritance

Protected Members

A protected member is inaccessible to all clients at all levels. If a protected data member is inherited, it is not accessible to the clients of this derived class, no matter what the access control is.

Protected items are things you need to write your functions, but you do not want any client to know about. For example, ListError.

Example

  • class base

    • private a // base’s client cannot use; cannot be passed down either
    • protected b // base’s client cannot use; can be passed down
    • public c // base’s client can use; can be passed down
  • class d1 : public base // inherits b and c from above publicly

    • // b is not accessible to d1’s client; b can be passed down
    • // c is accessible to d1’s client; c can be passed down
    • private x // d1’s client cannot use; cannot be passed down either
    • protected y // d1’s client cannot use; can be passed down
    • public z // d1’s client can use; can be passed down
  • class d2 : public d1 // inherits b and c, and y and z from above publicly

Protected Members

A protected member is inaccessible to all clients at all levels. If a protected data member is inherited, it is not accessible to the clients of this derived class, no matter what the access control is.

Protected items are things you need to write your functions, but you do not want any client to know about. For example, ListError.

Example

  • class base

    • private a // base’s client cannot use; cannot be passed down either
    • protected b // base’s client cannot use; can be passed down
    • public c // base’s client can use; can be passed down
  • class d1 : public base // inherits b and c from above publicly

    • // b is not accessible to d1’s client; b can be passed down
    • // c is accessible to d1’s client; c can be passed down
    • private x // d1’s client cannot use; cannot be passed down either
    • protected y // d1’s client cannot use; can be passed down
    • public z // d1’s client can use; can be passed down
  • class d2 : public d1 // inherits b and c, and y and z from above publicly

Protected Members

A protected member is inaccessible to all clients at all levels. If a protected data member is inherited, it is not accessible to the clients of this derived class, no matter what the access control is.

Protected items are things you need to write your functions, but you do not want any client to know about. For example, ListError.

Example

  • class base

    • private a // base’s client cannot use; cannot be passed down either
    • protected b // base’s client cannot use; can be passed down
    • public c // base’s client can use; can be passed down
  • class d1 : public base // inherits b and c from above publicly

    • // b is not accessible to d1’s client; b can be passed down
    • // c is accessible to d1’s client; c can be passed down
    • private x // d1’s client cannot use; cannot be passed down either
    • protected y // d1’s client cannot use; can be passed down
    • public z // d1’s client can use; can be passed down
  • class d2 : public d1 // inherits b and c, and y and z from above publicly