C++ Inheritance: Protected Member Access (Pt. 2)
Without inheritance, most classes typically use either public or private to specify the access level of their class members. However, if a derived class tried to interact with a private member of the base class, the compiler would throw an error complaining about a private member being accessed in the derived class. Example:
// Derived Class
class Wumpus : public Monster {
public:
...
void SelfDestruct() {
cout << "GG, Wumpus is outta here" << endl;
this->hearts = 0; // ERROR
}
private:
bool isVisible;
}
Since hearts
is specified as a private member on the base class, this member cannot be accessed directly by any of the methods of a derived class.
This is where the protected access level becomes useful. The protected member access level specifies that any derived class can access the protected member but nothing else. Example:
// The Base Class
class Monster {
public:
void setHealth(int hearts) {
this->hearts = hearts;
}
int getHealth() {
return this->hearts;
}
protected:
int hearts;
}
// Derived Class
class Wumpus : public Monster {
public:
void Hide() {
cout << "Shhhhhhh" << endl;
this->isVisible = false;
}
void Appear() {
cout << "ARGHHARHGHHHH" << endl;
this->isVisible = true;
}
void SelfDestruct() {
cout << "GG, Wumpus is outta here" << endl;
this->hearts = 0; // No Error
}
private:
bool isVisible;
}
int main() {
Monster base;
Wumpus derived;
derived.SelfDestruct() // No Error
base.hearts = base.hearts + 1; // Error, trying to modify protected member
cout << "Monster health: " << base.hearts << endl; // Also throws an error
return 0;
}
After moving int hearts
from private to protected in the base class, Monster
, the derived class, Wumpus
, can now interact with the attribute directly without making the compiler unhappy.
However, as shown when trying to access and modify the protected attribute in main()
, the compiler gets unhappy since only derived classes are allowed to interact with protected members of the base class.