In object-oriented programming (OOP), access control is a fundamental concept that defines how the members (attributes and methods) of a class can be accessed. C++ offers three levels of access control: public
, private
, and protected
. This guide will focus on the protected
access modifier and its implications in class hierarchy and access.
Protected members of a class are similar to private members, but with one significant difference—they can also be accessed by classes derived from it.
class Base {
protected:
int protectedVariable;
public:
Base() : protectedVariable(0) {}
void setProtectedVariable(int value) {
protectedVariable = value;
}
};
class Derived : public Base {
public:
void display() {
std::cout << "Protected Variable: " << protectedVariable << std::endl;
}
};
In the above example, Derived
class can access the protectedVariable
directly, because it is a protected member of its parent class Base
.
Here's a quick comparison:
public
: Members are accessible from anywhere where the object is visible.private
: Members are only accessible within the class they are declared and by friends of that class.protected
: Members are accessible within the class they are declared, by classes derived from it, and by friends of the class.
When deriving a class from a base class, the access specifier in the inheritance statement (public
, protected
, private
) determines the access to the base class members in the derived class.
class Base {
// ...
};
class DerivedPublic : public Base {
// Base's protected members become protected in DerivedPublic
// Base's public members become public in DerivedPublic
};
class DerivedProtected : protected Base {
// Base's protected and public members become protected in DerivedProtected
};
class DerivedPrivate : private Base {
// Base's protected and public members become private in DerivedPrivate
};
The above code snippet demonstrates how the inheritance type affects the access of the base class's members.
- Use
protected
when you want to hide the member from the world but still want to make it available to derived classes. - Prefer
private
toprotected
unless you are designing a class specifically intended to be subclassed. - Public inheritance (
class Derived : public Base
) should model an "is-a" relationship, whereas protected and private inheritance models "implemented in terms of" or "has a" relationships, respectively.
Protected members facilitate a controlled form of inheritance where derived classes are allowed more intimate access to the base class without exposing internals to the rest of the program. It's a balance between encapsulation and usability, critical for creating robust and maintainable class hierarchies. This guide aims to provide a clear understanding of protected members and how they impact class access in C++. Use this knowledge as you explore and create your own C++ class hierarchies.