0
0
C++programming~5 mins

Access control in inheritance in C++

Choose your learning style9 modes available
Introduction
Access control in inheritance helps decide which parts of a parent class can be used or changed by a child class. It keeps code safe and organized.
When you want to hide some details of a parent class from its child classes.
When you want child classes to use some parts of the parent class but not all.
When you want to control how child classes access or change parent class data.
When you want to protect important data from accidental changes in child classes.
Syntax
C++
class DerivedClass : access_specifier BaseClass {
    // members
};
access_specifier can be public, protected, or private.
It controls how the base class members are inherited by the derived class.
Examples
Public inheritance keeps public members public in the child.
C++
class Base {
public:
    int x;
};

class Derived : public Base {
    // x is public here
};
Protected inheritance keeps public and protected members protected in the child.
C++
class Base {
protected:
    int y;
};

class Derived : protected Base {
    // y is protected here
};
Private inheritance makes all inherited members private in the child.
C++
class Base {
private:
    int z;
};

class Derived : private Base {
    // Base members are private here
};
Sample Program
This program shows how public, protected, and private inheritance affect access to base class members in derived classes and outside them.
C++
#include <iostream>
using namespace std;

class Base {
public:
    int a = 10;
protected:
    int b = 20;
private:
    int c = 30;
};

class PublicDerived : public Base {
public:
    void show() {
        cout << "a = " << a << "\n";   // accessible
        cout << "b = " << b << "\n";   // accessible
        // cout << "c = " << c << "\n"; // not accessible, private in Base
    }
};

class ProtectedDerived : protected Base {
public:
    void show() {
        cout << "a = " << a << "\n";   // accessible
        cout << "b = " << b << "\n";   // accessible
    }
};

class PrivateDerived : private Base {
public:
    void show() {
        cout << "a = " << a << "\n";   // accessible
        cout << "b = " << b << "\n";   // accessible
    }
};

int main() {
    PublicDerived pd;
    pd.show();
    cout << "pd.a = " << pd.a << "\n"; // accessible

    ProtectedDerived prd;
    prd.show();
    // cout << prd.a; // error: 'a' is protected in ProtectedDerived

    PrivateDerived pvd;
    pvd.show();
    // cout << pvd.a; // error: 'a' is private in PrivateDerived

    return 0;
}
OutputSuccess
Important Notes
Private members of the base class are never accessible directly in derived classes.
Public inheritance models 'is-a' relationship, keeping access levels mostly unchanged.
Protected inheritance hides public members from outside but keeps them accessible to derived classes.
Private inheritance hides all base class members from outside and derived classes.
Summary
Access control in inheritance controls how base class members are seen in derived classes.
Public inheritance keeps public members public, protected inheritance makes them protected, and private inheritance makes them private.
Private members of the base class are always hidden from derived classes.