What is the difference between public, protected and private inheritance in C++?
Hello, colleagues! I started learning C++ and encountered confusion with inheritance. I understand there are three access specifiers: public, protected and private. But when I try to apply them to inheritance, I get lost. For example, I have a base class:
```cpp
class Base {
public:
int pub = 1;
protected:
int prot = 2;
private:
int priv = 3;
};
```
I want to create a Derived class that inherits from Base. I tried writing `class Derived : public Base`, `class Derived : protected Base` and `class Derived : private Base`. In each case the behavior is different, but I cannot understand what exactly the difference is. Why, for example, with private inheritance I cannot access pub from outside? And what is "accessibility" of the derived class's members?
I feel like I'm missing some fundamental concept.
No answers yet.
Log in to leave an answer