Concepts of Programming Languages

(Sean Pound) #1

514 Chapter 11 Abstract Data Types and Encapsulation Constructs


following sections, we briefly describe the uses of naming encapsulations in
C++, Java, Ada, and Ruby.

11.7.1 C++ Namespaces


C++ includes a specification, namespace, that helps programs manage the
problem of global namespaces. One can place each library in its own namespace
and qualify the names in the program with the name of the namespace when
the names are used outside that namespace. For example, suppose there is an
abstract data type header file that implements stacks. If there is concern that
some other library file may define a name that is used in the stack abstract data
type, the file that defines the stack could be placed in its own namespace. This
is done by placing all of the declarations for the stack in a namespace block, as
in the following:

namespace myStackSpace {
// Stack declarations
}

The implementation file for the stack abstract data type could reference
the names declared in the header file with the scope resolution operator,
::, as in

myStackSpace::topSub

The implementation file could also appear in a namespace block specifica-
tion identical to the one used on the header file, which would make all of the
names declared in the header file directly visible. This is definitely simpler, but
slightly less readable, because it is less obvious where a specific name in the
implementation file is declared.
Client code can gain access to the names in the namespace of the header
file of a library in three different ways. One way is to qualify the names from
the library with the name of the namespace. For example, a reference to the
variable topSub could appear as follows:

myStackSpace::topSub

This is exactly the way the implementation code could reference it if the imple-
mentation file was not in the same namespace.
The other two approaches use the using directive. This directive can be
used to qualify individual names from a namespace, as with

using myStackSpace::topSub;

which makes topSub visible, but not any other names from the myStackSpace
namespace.
Free download pdf