In C++ the access specifier before a base class, public, protected or private, decides how the base's members appear in the derived class and to the outside world. Public inheritance models an "is-a" relationship, while protected and private inheritance hide the base interface and model "implemented in terms of". This guide covers the types of inheritance c++ offers, what each one changes and which to choose.
The specifier never gives a derived class more access than the base granted; it can only keep or tighten it. A private member of the base stays inaccessible to the derived class in every case.
You write the specifier in the base list:
For a class, omitting the specifier means private. For a struct, it means public. Always write it out so the intent is visible.
This table shows how members of the base are seen inside the derived class after inheritance:
| Base member | public inheritance | protected inheritance | private inheritance |
|---|---|---|---|
public | public | protected | private |
protected | protected | protected | private |
private | inaccessible | inaccessible | inaccessible |
The rule is simple: the effective access is the more restrictive of the member's own access and the inheritance mode. A further class derived from the derived class sees the results in the table, which is why protected and private inheritance affect grandchildren as well.
Public inheritance keeps the base interface public. A Derived object can be used wherever a Base is expected, including through pointers and references.
Both calls compile because the conversion from Circle& to Shape& is accessible. Use public inheritance only when the Liskov substitution principle holds: every Circle really can stand in for a Shape without surprising callers.
With protected inheritance the base's public and protected members become protected in the derived class. Outside code cannot call them, but further derived classes can.
Protected inheritance is rare. It fits a hierarchy where a family of classes shares an implementation base but outsiders must not treat them as that base.
Private inheritance makes everything from the base private in the derived class. The derived class can use the base, but no one else can, and the implicit conversion to the base is blocked outside the class.
Stack reuses std::vector<int>, yet users cannot call insert or index into it. In most cases composition is clearer: hold a std::vector<int> as a member. Choose private inheritance only when you need something composition cannot do, namely overriding a virtual function of the base, accessing a protected member, or benefiting from the empty base optimisation (C++20 also offers [[no_unique_address]] for members, which covers the last case).
You can re-expose selected names with a using declaration:
| Situation | Use |
|---|---|
| Derived is a kind of base, used polymorphically | public |
| Reuse an implementation, hide the interface | composition first, private if needed |
| Hide the base from users but share it with subclasses | protected (rare) |
Default to public inheritance for real is-a relationships and composition for everything else.
Conversions from derived to base pointers follow the same access rules. With non-public inheritance, Base* p = &derived; fails outside the class. A C-style cast can bypass this check and is a classic source of bugs, so avoid it and prefer static_cast, which respects access.
Also note that access control applies to names, not visibility. A private base member is still part of the object in memory; it just cannot be named from the derived class.
private for a class and public for a struct. Write the specifier explicitly so readers do not have to remember this.
No, not under any inheritance mode. Make the member protected or give the base a public or protected accessor.
It serves a similar purpose, but private inheritance also lets you override virtual functions and reach protected members of the base. Composition is simpler and should be the first choice.
No. The mode changes only compile-time access checks, not the memory layout.
Public inheritance keeps the base interface and expresses is-a. Protected and private inheritance restrict that interface and express implemented-in-terms-of, with private being the more common of the two. The effective access of an inherited member is never looser than the inheritance mode, and private base members stay inaccessible. Prefer public inheritance for polymorphism and composition for reuse.