class Person
{
protected:
    string name;   // accessible to Person AND any derived class, not from main()
public:
    Person(string n) : name(n) {}
};

class Student : public Person
{
public:
    Student(string n) : Person(n) {}
    void show() { cout << name; }   // OK -- name is protected, Student can access it
};

// int main() { Student s("Ali"); cout << s.name; }  --> ERROR: name is protected, not accessible outside

The three access levels, side by side

class Base
{
private:   int a;   // Base only
protected: int b;   // Base + derived classes
public:    int c;   // everyone
};

class Derived : public Base
{
public:
    void test()
    {
        // a = 1;   // ERROR -- private, not accessible even in derived class
        b = 2;      // OK -- protected is accessible in derived classes
        c = 3;      // OK -- public is always accessible
    }
};

int main()
{
    Derived d;
    // d.a;   // ERROR
    // d.b;   // ERROR -- protected not accessible from OUTSIDE the class hierarchy either
    d.c;      // OK -- public
}

The rule, stated precisely

protected is accessible from: (1) the class that declares it, and (2) any class that derives from it — but not from ordinary outside code, even code that has an object of the derived class. This is the detail students most often get wrong: protected is not "public within the family and private outside" — it's still fully blocked from any code outside the class hierarchy.

Real-world use case

class Employee
{
protected:
    double baseSalary;   // subclasses need this to calculate their own pay, outside code doesn't
public:
    Employee(double s) : baseSalary(s) {}
};

class Manager : public Employee
{
private:
    double bonus;
public:
    Manager(double s, double b) : Employee(s), bonus(b) {}
    double totalPay() { return baseSalary + bonus; }   // uses protected base data directly
};

Important MCQs

  1. A protected member is accessible from:
  2. If main() creates a Student object (derived from Person, where name is protected), can main() directly access s.name?
  3. protected sits, in terms of visibility, between:

Self-Test Questions

  1. Can outside code (e.g., main()) access a protected member through a derived-class object? Why or why not?
  2. Why is protected a better choice than private for baseSalary in the Employee/Manager example?
  3. What's the practical difference between making baseSalary protected vs public?

📌 Sample & Repeated FPSC Questions (2016–2026)

2026, Paper I, Q4(c) (8 marks) — Demonstrate method overriding using a base class Account and a derived class Current Account. Make balance a protected data member.

class Account
{
protected:
    double balance;
public:
    Account(double b) : balance(b) {}
    virtual void withdraw(double amount)
    {
        if (amount <= balance) { balance -= amount; }
        else cout << "Insufficient funds.\n";
    }
};

class CurrentAccount : public Account
{
    double overdraftLimit;
public:
    CurrentAccount(double b, double od) : Account(b), overdraftLimit(od) {}
    void withdraw(double amount) override      // overriding
    {
        if (amount <= balance + overdraftLimit) balance -= amount;   // uses inherited protected member
        else cout << "Exceeds overdraft limit.\n";
    }
};