Access specifier in C++ classes used as access control:
Private access specifier:
All member of a class are restricted to be accessed only by members of the same class and friend function and members of friend class.
Public access specifier:
All member of a class are free to be accessed by anyone, anywhere within and outside its class.
Protected access specifier:
All member of a class are restricted to be accessed only by members of the same class and its direct or indirect derived classes and friend function and members of friend class
Beside assigning access specifier to members in a class, members of superclass will have different access specifier in a subclass depending on the access specifier used while derivation:
Public:
When base class derived publicly, public and protected members of the base class remain public and protected members of the derived class. Private members of base class are not inherited in derive class
Private:
When base class derived privately, public and protected members of the base class will become private members in derive class. Private members of base class are not inherited in derive class
Protected:
When base class derived protectedly, public and protected members of the base class will become protected members in derive class. Private members of base class are not inherited in derive class.
An Example:-
#include<iostream>
using namespace std;
class base
{
int x;
public:
int y;
protected:
int z;
public:
void base_show()
{
cout<<x<<y<<z;
}
};
//derivation using public derivation mode........................
class child:public base
{
public:
void child_show()
{
base_show();
}
};
class child1:public child
{
public:
void child1_show()
{
child_show();
}
};
int main()
{
child1 ch1;
ch1.child1_show();
return 0;
}
Comments