The difference between public private and protected inheritance

Understanding the differences between public, private, and protected inheritance in C++ is crucial for designing robust and maintainable object-oriented systems. Here’s a the most concise explanation of these concepts and their implications, using code segments.


class A 
{
    public:
       int x;
    protected:
       int y;
    private:
       int z;
};

class B : public A
{
    // x is public
    // y is protected
    // z is not accessible from B
};

class C : protected A
{
    // x is protected
    // y is protected
    // z is not accessible from C
};

class D : private A    // 'private' is default for classes
{
    // x is private
    // y is private
    // z is not accessible from D
};