Data Hiding -- Private Data

The class is an instance of an abstract data type (or ADT) which combines both data and the functions that operate on the data. The data is commonly kept in the private area of the class (to keep the general user from modifying it) and is part of the implementation of the class.

For example, the implementation of a 3-dimensional vector class needs 3 required components - the x, y, and z-coordinate. There are several methods by which these three values can be defined - a three-element array, three individual elements, etc. - and each affects the way that the implementation is written. An implementation accomplished through the definition of three individual values in the private area of the class could be written


All member and friend function of this class manipulate these three values.

Note: We commonly utilize underscores as a prefix to our private data. They are a common software engineering tool utilized in C++ to distinguish private data from other variables in the implementation code.

We adhere, in the design of concrete data types, to rule 20 of Meyers which states ``avoid data members in the public interface''. This gives the designer much more flexibility in the implementation of a class. If a data member is made public, then every user has access to it and can modify it - and the design of the operators in the class must take this into consideration. For example, if it were necessary to change the implementation of the class, the fact that many applications may have utilized this data item may make it impossible to change the class without changing the interface - not desirable if this class is highly utilized (Consider that the data item could not be replaced with a function without adding a set of parenthesis to every applications programmer's use of this variable).

If the applications programmer needs access to the private data items, then access functions should be provided. In the case of the Vector class above, access functions are provided to allow the user to utilize the x, y, and z coordinates.

In general, no data item should ever be placed in the public interface. The data can be made public by utilizing access functions - and only member or friend functions should be allowed to change the data. If we follow these ideas, and later wish to change the implementation of the class then each of the member functions could be changed to reflect this new implementation, without changing the interface to the user.

  Public Area of the Interface

The public area of the interface includes all functions that are publicly accessible to the users of this class. Normally, using the true encapsulation and information hiding principles of abstract data types, this will not include the data that is specific to the implementation of the class. Such data should only be contained in the private or protected areas of the interface.

This document maintained by Ken Joy.
Comments to the author : joy@cs.ucdavis.edu

All contents copyright (c) 1998
Computer Science Department, University of California, Davis
All rights reserved.

Ken Joy
Wed Jan 7 14:57:50 PST 1998