ASSIGNING ACCESS RIGHTS

Every member of the class can have on of three possible ways in which surrounding program code can access them and these types of access are:
-          Public,
-          Private and
-          Protected.
The access rights are given in the way to specify one of three keywords and these keywords are: public, private and protected after each of these keywords we have to put colon.
Let’s take a look at the following example.
#include "stdafx.h"
#include "iostream"
using namespace std;
class Access{
public:
       int a, b, c;
       void function1(int counter);
private:
       int d;
protected:
       int e;
       void function2();
};
int _tmain(int argc, _TCHAR* argv[])
{
       return 0;
}

The access rights determines which class members will be available from outside of the class, which will be available from inherited classes and which will be available only inside the class. The programmer specifically determines the audience for specific part of the objects.

PUBLIC ACCESS: The public access is given with keyword public. The members which have public access right can be accessed from outside of the class. The integer elements a, b, c and function1 can be accessed from outside but we need to define some object of the class Access and then using operator . we can access them.

PRIVATE ACCESS: The member d has private access which is defined using keyword private. In program code outside the class and in the inherited classes the member d is not available. We can access to member d only using functional class members of class Access.

PROTECTED ACCESS: Protected class members are defined using keyword protected and using that keyword we limit the access to the member e and functional member named function2(). We can’t access them using object. They are available only to the functional members of the class Access and classes which are inherited form class Access.
Now, using previous explanation we can create objects and give some example of how we can access to specific members of class Access.

#include "stdafx.h"
#include "iostream"
using namespace std;

class Access{
public:
       int a, b, c;
       void function1(int counter);
private:
       int d;
protected:
       int e;
       void function2(int counter2);
};
Access x;
void Access::function1(int counter){
       a = counter;
       c = a + a + 5;
       void function2();
}
int _tmain(int argc, _TCHAR* argv[])
{
       //This is OK because a and b have public access
       x.a = x.b = x.c = 6;
       // and this is also OK because function1(int counter) have
       //public access
       x.function1(1);
       //This is not correct because d have private access and we
       //can't access him from outside
       cout << x.d << endl;
       //This is not correct because function2() have protected
       //access
       x.function2();
       return 0;
}

If you run the previous code you would get an error because:
-          you can’t access the variable d using object because it has a private access and
-          You can’t access the function named function2() because the function2() have protected access
So if you delete these two lines you should be able to execute this program code.

IMPORTANT: If we don’t write the access rights explicitly then the members inside the class have private access.
Share on Google Plus