Notes

Thoughts, write-ups, and things worth remembering.

Inheritance basics

#cpp#oop Updated Sep 28, 2026

A derived class gets everything a base class has, plus whatever new it adds.

class Shape {
public:
    void printName() { std::cout << name; }
protected:
    std::string name;
};

class Circle : public Shape {
public:
    Circle() { name = "Circle"; }
    float radius;
};

Circle has printName() and name for free, without rewriting either.

public / protected / private inheritance

class A : public B { };      // B's public stays public, protected stays protected
class C : protected B { };   // B's public AND protected become protected in C
class D : private B { };     // B's public AND protected become private in D

public inheritance (“is-a”) is what you want almost always. protected/private inheritance (“implemented-in-terms-of”) shows up rarely and usually means composition would’ve been the cleaner choice.

Members and access control