oops · high

Access Specifiers & Inheritance Modes

C++ controls access along two independent axes. The access specifier on a member sets its visibility: public is visible to everyone, protected to the class and its derived classes, and private only to the class itself (not even derived classes). The inheritance mode (: public/protected/private Base) caps how the base's members are re-exposed in the derived class — public inheritance keeps public/protected as-is, protected inheritance lowers public to protected, and private inheritance makes everything private; the mode can only lower visibility, never raise it, and a private base member is never accessible in the derived class. Crucially, only public inheritance models an IS-A relationship and permits upcasting a Derived* to a Base*; private and protected inheritance mean 'implemented in terms of', so reach for them only for implementation reuse and use public inheritance (or composition) for genuine IS-A.

🔑 Key line

Two axes: access specifiers (public/protected/private) set MEMBER visibility — everyone / class+derived / class-only; the inheritance MODE (: public/protected/private Base) caps how inherited members are re-exposed and can only LOWER visibility. Only public inheritance models IS-A and allows upcasting Derived*->Base*.

The code

struct Base {
public: int pub; // visible to EVERYONE
protected: int prot; // Base + derived classes only
private: int priv; // Base only (not even derived)
};
struct Pub : public Base {}; // pub->public, prot->protected
struct Pro : protected Base {}; // pub->protected (lowered)
struct Pri : private Base {}; // pub->private (all become private)
Base* b = new Pub; // upcast OK — public inheritance models IS-A
// Base* x = new Pri; // ERROR: private inheritance is NOT IS-A

What this lesson walks through

  1. 01Access specifiers control MEMBER visibility
  2. 02Inheritance MODE caps how members are re-exposed
  3. 03Only public inheritance is IS-A (allows upcasting)

Inside a class, the access specifier on each member decides who may touch it. public members are visible to everyone; protected members are visible to the class and its derived classes; private members are visible only to the class itself — not even to derived classes. This is the first axis: visibility of a member.

See it animated — step by step, at your own pace

Unlock the full interactive walkthrough of Access Specifiers & Inheritance Modes and 100+ animated C++ interview lessons.

← Previous
The Four Pillars of OOP
Next →
The Diamond Problem & Virtual Inheritance