Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
452 CHAPTER 10 User-Defined Data Types This chapter represents a transition point in your study of computer science and C++ pro- gramming. So far, we have emphasized simple variables, control structures, and named processes (functions). After this chapter, the focus shifts to ways to structure (organize) data and to the algorithms necessary to process data in these structured forms. To make this transition, we must examine the concept of data types in greater detail. Until now, we have worked primarily with the data types , , , and . These four data types are adequate for solving a wide variety of problems. Certain programs, however, need other kinds of data. Sometimes the built-in data types cannot adequately represent all the data in a program. C++ has several mechanisms for creating user-defined data types; that is, we can define new data types ourselves. This chapter introduces one of these mechanisms, the enumeration type. In this chapter, we also expand the definition of a data type to include structured types, which represent collections of components that are referred to by a single name. We begin with a discussion of structured types in general and then examine two structured types provided by the C++ language: and . 10.1 Built-In Simple Types In Chapter 2, we defined a data type as a specific set of data values (which we call the do- main) along with a set of operations on those values. For the type, the domain is the set of whole numbers from through , and the allowable operations we have seen so far are , , , , , , , and the relational and logical operations. The domain of the type is the set of all real numbers that a particular computer is capable of repre- senting, and the operations are the same as those for the type except that modulus ( ) is excluded. For the type, the domain is the set consisting of the two values and , and the allowable operations are the logical ( , , ) and relational operations. The type, although used primarily to manipulate character data, is classified as an integral type because it uses integers in memory to stand for characters. Later in the chapter we will see how this process works. The , , , and types have a property in common: The Simple (atomic) data type A domain of each type is made up of indivisible, or atomic, data values. Data data type in which each value is types with this property are called simple (or atomic ) data types . When we say that atomic (indivisible). a value is atomic, we mean that it is not defined to have component parts that are accessed separately. For example, a single character of type is atomic, but the string "Good Morning" is not (it is composed of 12 individual characters that we can also access). Another way of describing a simple type is to say that only one value can be associated with a variable of that type. In contrast, a structured type is one in which an entire collec- tion of values is associated with a single variable of that type. For example, a string object represents a collection of characters that are given a single name. FIGURE 10.1 shows the simple types that are built into the C++ language. This figure is a portion of the complete diagram of C++ data types presented in Figure 3.1. In this figure, one of the types-- --is not actually a single data type in the sense that and are data types. Instead, it is a mechanism with which we can define our own simple data types. We will look at later in this chapter.