Protected inheritance is useful when you want to derive a new class from an existing base class, but you don't want to expose all of the members of the base class to the derived class or to the outside world.
For example, consider a class hierarchy for different types of shapes. The base class may have a protected member variable for the color of the shape, as well as public member functions for getting and setting the color. A derived class for circles may want to inherit this color functionality, but it may not want to expose the color to the outside world as a public member.
In this case, the derived class could use protected inheritance to inherit the color functionality from the base class, while keeping the color variable itself protected. This way, the derived class can still use the color variable internally, but it's not exposed to code outside of the class hierarchy.
Another example is a base class for a widget in a graphical user interface. The base class may have protected member variables for the size and position of the widget, as well as public member functions for getting and setting these values. A derived class for a button may want to inherit these size and position members, but it may not want to expose them as public members to the outside world. Protected inheritance can be used in this case to inherit the size and position functionality while keeping the actual variables protected.
In general, protected inheritance is useful when you want to inherit some of the functionality of a base class without exposing all of its members to the derived class or to code outside of the class hierarchy.
People can sometimes confuse the concepts of protected, public, and private in C++ because they all deal with the visibility of data and functions in a class hierarchy, and their use can be somewhat subjective depending on the specific design goals and requirements of a program.
Additionally, the syntax used in C++ to denote the different access levels can be a bit confusing, particularly for beginners. For example, it may not be immediately clear what the difference is between protected
and private
, or why one would use public
inheritance instead of private
inheritance.
Another reason for confusion is that the access level of a member function or data member can have a cascading effect on the accessibility of other members in the class hierarchy. For example, if a member function is marked as protected
, then any derived classes can access that function as well, even if they are not marked as protected
. This can make it difficult to reason about the accessibility of members in complex class hierarchies.
Finally, some programmers may not fully understand the implications of using different access levels, or may not have a clear understanding of the design goals of the program they are working on, which can lead to incorrect or inefficient use of access levels.
#include <iostream>
using namespace std;
class Prim{
protected:
string name;
public:
void setName(string x) {
name = x;
}
};
class Sec : protected Prim{
public:
void printName() {
cout << "Name from Prim Cls: " << name << endl;
}
void caller(string x) {
setName(x);
}
};
int main() {
Sec Object;
Object.caller("Samantha");
Object.printName();
return 0;
}
The Sec
class inherits from the Prim
class with the protected
keyword. This means that all public and protected members of the Prim
class become protected members of the Sec
class. Therefore, Sec
can access the protected name
member of Prim
, but other classes cannot access it.
The printName()
method of Sec
class accesses the name
member and displays it to the console. The caller()
method of Sec
class calls the setName()
method of the Prim
class to set the value of name
.
No comments:
Post a Comment