protected members are accessible within the defining class and any class derived from it, but not from outside code (unlike public) and not from unrelated classes (unlike... well, nothing else grants this specific visibility)private (class-only) and public (everyone) — the natural choice for data a base class wants to expose to its subclasses onlyclass 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
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
}
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.
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
};
protected member is accessible from:
main() creates a Student object (derived from Person, where name is protected), can main() directly access s.name?
protected sits, in terms of visibility, between:
main()) access a protected member through a derived-class object? Why or why not?protected a better choice than private for baseSalary in the Employee/Manager example?baseSalary protected vs public?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";
}
};