Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What Are Public, Protected and Private Inheritance in C++?

C++
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedDec 6, 2026
6 min read

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.

Table of Contents

  • The inheritance access specifier
  • How each mode changes member access
  • Public inheritance and the is-a relationship
  • Protected inheritance
  • Private inheritance and implemented-in-terms-of
  • Which mode to choose
  • Casting and access pitfalls
  • Frequently Asked Questions
    • What is the default inheritance access in C++?
    • Can a derived class access private members of its base?
    • Is private inheritance the same as composition?
    • Does inheritance mode affect object size?
  • Summary
  • Related Articles

The inheritance access specifier

You write the specifier in the base list:

cpp

For a class, omitting the specifier means private. For a struct, it means public. Always write it out so the intent is visible.

How each mode changes member access

This table shows how members of the base are seen inside the derived class after inheritance:

Base memberpublic inheritanceprotected inheritanceprivate inheritance
publicpublicprotectedprivate
protectedprotectedprotectedprivate
privateinaccessibleinaccessibleinaccessible

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 and the is-a relationship

Public inheritance keeps the base interface public. A Derived object can be used wherever a Base is expected, including through pointers and references.

cpp

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.

Protected inheritance

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.

cpp

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 and implemented-in-terms-of

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.

cpp

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:

cpp

Which mode to choose

SituationUse
Derived is a kind of base, used polymorphicallypublic
Reuse an implementation, hide the interfacecomposition first, private if needed
Hide the base from users but share it with subclassesprotected (rare)

Default to public inheritance for real is-a relationships and composition for everything else.

Casting and access pitfalls

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.

Frequently Asked Questions

What is the default inheritance access in C++?

private for a class and public for a struct. Write the specifier explicitly so readers do not have to remember this.

Can a derived class access private members of its base?

No, not under any inheritance mode. Make the member protected or give the base a public or protected accessor.

Is private inheritance the same as composition?

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.

Does inheritance mode affect object size?

No. The mode changes only compile-time access checks, not the memory layout.

Summary

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.

Related Articles

  • What Is Inheritance in C++ and How Does It Work?
  • How Does Multiple Inheritance Work in C++?
  • Learn C++: Complete Study Roadmap from Beginner to Advanced
Share

Similar posts

  • How Do = default and = delete Work for Special Members in C++?
  • What are nested classes in C++ and when do you use them?
  • What are default member initializers and aggregates in C++?
  • How do brace initialization and initializer_list work in C++?
  • What are static data members in C++ classes?
  • What is the mutable keyword in C++ and when do you need it?
  • What are const member functions in C++?
  • What is the explicit keyword in C++ and why use it?