In some of the previous articles we mentioned the keywords
public, and we said that they are called access modifiers, used to control who can access which parts of the code. Now we will discuss them in greater detail, but first, let's make a short introduction that will help us understand the importance of access control.
One of the concepts on which object-oriented programming was founded is called information hiding. Information hiding is the principal of separating the functional parts of the code from the user interface, so that the very core of the program is not changed when some design modifications are made. This way, certain restrictions are introduced for the client: the programmer can (and most likely will) make some parts of the code unreachable for the user, in order to prevent accidental corruption.
Another term related to information hiding is encapsulation. (There have been debates over the differences between the concepts of information hiding and encapsulation: we will consider that encapsulation is one of the ways of achieving information hiding.) Simply speaking, encapsulation means "packing" of data and functions into a single module. In C++, Java, C# and other object-oriented languages, this is achieved through classes.
Encapsulation can be used to hide data members and some methods. Typically, only the object's own methods can directly inspect or manipulate its fields.
Now we will see how the keywords can be used to specify which code segments the user can access, and which not.
A C++ class can have multiple sections labeled
private. Each section remains in effect until another section label is seen.
Note: The default access for members is
First we will compare the
A public member is accessible from anywhere outside the class but within a program. You can set and get the value of public variables directly.
A private member variable or function cannot be accessed, or even viewed from outside the class. Only the class and friend functions can access private members (we will talk about the
friend keyword a bit later).
To illustrate this, we will use the same example that we used in one of the previous articles.
Now, we can write something like:
city1.name = "Athens";
since the modifier for the
But this is not the usual way of implementing a class. Following the rule of information hiding, the data members are usually declared
private, so that they can't be accessed directly, like in the previous example.
However, there should be a way for the user to modify and obtain those values, and this is achieved through functions usually called getters and setters.
public: //methods (constructors, too!) are usually declared public
string getName() //obtaining the value of name member
void setName(string n) //a way to modify the name member
name = s;
/*getters and setters for other data members*/
city1.name = "Barselona"; //error - name is private
city1.setName("Barselona"); //correct way
The third access modifier we mentioned was
protected. The level of protection that
protected offers is the same as the
private modifier, with the difference that the members declared
protected can also be accessed from the child classes (classes which inherit our class). We will see an example of using this keyword when we talk about inheritance. For now, it's just important to understand the private/public concept and why it matters.