Using Access Specifiers To Control Horizontal Member Access

 < 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.

The Concept of Horizontal Access

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.

click to expand
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

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).

Access Specifiers

The three access specifiers are briefly discussed below in the context of horizontal access.

The Public Access Specifier

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.

The Protected Access Specifier

Protected accessibility prevents horizontal access. Horizontally speaking, protected access is the same as private access.

The Private Access Specifier

Private accessibility prevents horizontal access.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net