| < Day Day Up > |
|
There are three access specifiers: public, protected, and private. They are all used to control both horizontal and vertical access to an object’s data and function members. This section deals exclusively with horizontal access; vertical access is discussed in detail in chapter 13.
Horizontal access is the access an object of a particular class type has to the data members and member functions of another object of a different class type. Figure 11-8 gives an illustration.
Figure 11-8: Horizontal Access
Object A could actually be something other than an object. For instance, when in a main() function, an object of, say, SimpleClass is created and one of SimpleClass’s public functions is invoked, then the main() function is horizontally accessing an object of type SimpleClass. The object requesting the access is considered the client object; the object providing the access is considered the server object.
Data encapsulation is the act of hiding an object’s data members from the outside world. As shown in figure 11- 8, access to the functionality of an object should be through a set of functions or methods, such as accessor functions, mutator functions, or one of the special functions. At no time should a client object be allowed to muck around with a server object’s private parts. The only information a client object needs to know about a server object to use it effectively is its public interface. A set of public interface functions is referred to as an Application Programming Interface (API).
The three access specifiers are briefly discussed below in the context of horizontal access.
Public accessibility enables horizontal access. Any data member or member function declared as being public is horizontally accessible by client objects. Generally speaking, the only parts of an object that should have public accessibility are its authorized public interface functions.
Protected accessibility prevents horizontal access. Horizontally speaking, protected access is the same as private access.
Private accessibility prevents horizontal access.
| < Day Day Up > |
|