Download Practical C Programming - International Research Institute MICA

Transcript
Public and Private
305
Also, as programming problems get bigger, more and more programmers are
needed to finish them. An efficient way of splitting up a large project is to assign
each programmer a different module. In this manner, each programmer only
worries about the internal details of a particular module.
In this chapter, we will discuss a module to handle infinite arrays. The functions in
this package allow the user to store data into an array without worrying about its
size. The infinite array grows as needed (limited only by the amount of memory in
the computer). The array will be used to store data for a histogram, but can be used
to store things like line numbers from a cross-reference program or other types of
data.
Public and Private
Modules are divided into two parts: public and private. The public part tells the
user how to call the functions in the module. It contains the definition of data
structures and functions that are to be used outside the module. These definitions
are put in a header file, and the file must be included in any program that
depends on that module. In our infinite array example, we have put the public
declarations in the file ia.h, which we will look at shortly. Figure 18-1 illustrates
the relationship between the various parts of the infinite array package.
Public
Definitions imported through
#include ia.h directive
ia.h
Definition of ia
hist.c
Private
ia.c
Use of ia
Implementation of ia
Figure 18-1: Definition, implementation, and use of the infinite array
Anything that is internal to the module is private. Everything that is not directly
usable by the outside world should be kept private.
One of the advantages of C++ over C is that you can explicitly declare what is
public and what is private, and prevent unauthorized modification of private data.