Skip to main content

Section 16.4 Information Hiding

Subsection 16.4.1 Access Modifiers

In a class, the keywords private and public are known as access modifiers and are used to define what parts of a class are visible to code outside of that class. Using public: in a class says “everything from here until the next access modifier is visible”. Using private: “everything from here until the next access modifier is hidden”.
class Sample {
private:
    int m_variable1;  // private
    int m_variable2;  // private
public:
    int m_variable3;  // public
    int m_variable4;  // public
    int m_variable5;  // public
private:
    int m_variable6;  // private
};
If you do not specify an access modifier, the default modifier applied to members of a class is private. So the reason Listing 16.3.3 did not work is because the compiler assumed the variables should be private:
Listing 16.4.1.
Because the compiler assumed the variables should be private, the code in main (line 10) that tries to access m_x directly results in a compilation error. It is a syntax error for code outside of a class to try to access members of that class that were defined in a private block.
We could make the code compile by changing line 4 to say public:. Go ahead and try it.

Subsection 16.4.2 Why use Private

So why do we bother with private member variables at all? Why not just make everything public and let code access whatever it wants?
To understand why, we need to remember what object-oriented programming was designed to accomplish. It was designed to help make it easier to write and use code in large programs. Part of how it supports this is by giving us tools to manage the complexity of code.
One form of complexity is the coupling of code. We say that code is tightly coupled when one part depends on all of the details of other parts of the code. Loosely coupled coupled code is the opposite. It is when any given part of the code depends on as few details of other parts as possible.
Say we are working with a Point class. If code outside of the Point class is directly accessing the member variables m_x and m_y, then that code is tightly coupled to the implementation details of the Point class. It depends on knowing that Point stores its data as two double values named m_x and m_y.
That has two consequences:
  • First, if we want to change how Point stores its data, we have to find all of the code that is directly accessing m_x and m_y and change that code as well.
  • Second, the programmer who is working on the code that is using Point objects has to understand all of the details of how Point works. They have to know that Point has member variables named m_x and m_y, what those variables mean, what values are valid for them, etc...
Making the member variables private helps to enforce this boundary. Outside code is not allowed to tightly couple with the details of the Point class.

Insight 16.4.1.

Remember to imagine every object-oriented program as being written by multiple programmers. The programmer who is working on the Point class might be different than the programmer who is working on the code that is using Point objects. By making member variables private, the Point programmer can enforce a boundary between the code that “knows the details” of how Point works and the code that does not know or depend on those details.

Checkpoint 16.4.1.

Which is considered to generally lead to better software design?
  • Loosly coupled code.
  • Correct. Loosely coupled code is easier to work on and maintain. A change in one part of the code is less likely to break other parts of the code.
  • Tightly coupled code.
  • Tightly coupled code is harder to work on and maintain. A change in one part of the code is more likely to break other parts of the code.
  • Uncoupled code.
  • That is not a thing.

Checkpoint 16.4.2.

What are some of the benefits of information hiding?
  • It keeps code secret from other programmers and users.
  • Incorrect! “hiding” is not a great term for what we are really doing. “protecting” might be a better description. The code is still there and can be read by other programmers. They just can’t access it directly.
  • It makes it easier to change implementation details of a class without breaking code that uses the class.
  • It allows us to enforce boundaries between code that “knows the details” of a particular type and code that does not know or depend on those details.
You have attempted of activities on this page.