PROTECTED MEMBERS OF CLASS
Protected members are the members that can be used only by the member function and the friends of the class. Just like the private members they can be accessed by non-member functions.
The difference comes when we talk about inheritance. protected members are inheritable but private members are non-inheritable.
SCOPE
Protected members of a class have class scope that means these can only be accessed by the member functions of the class.Example:
class A{
private:
int a;
void test()
{
cout<<a;
}
protected: int b;
public:
int i;
void show()
{
cout<<i+5;
a=9; //This is valid because the private memebr can be used by the member functions of that class
test(); //Valid. Just like the variable, member function can also be accessed by the public members of that class
}
};
A obj; //object created
void main()
{
clrscr();
obj.show(); //Valid
obj.i= 20; //Valid
obj.a=40; //Invalid. a is private
obj.test(); //Invalid
obj.b=7; //Invalid. b is protected
getch();
}
No comments:
Post a Comment