• No results found

In the early days, computers were designed to work only with numeric data and were sometimes called number crunchers as a result. Modern computers, however, work less with numeric data than they do with text data, that is, any information composed of individual characters that appear on the keyboard and the screen. The ability of modern computers to process text data has led to the development of word processing systems, online reference libraries, electronic mail, social networks, and a seemingly infinite supply of exciting applications.

The most primitive elements of text data are individual characters, which are represented in C++ using the predefined data type char. The domain of type char is the set of symbols that can be displayed on the screen or typed on the keyboard: the letters, digits, punctuation marks, spacebar, RETURN key, and so forth. Internally, these values are represented inside the computer by assigning each character a numeric code. In most implementations of C++, the coding system used to represent characters is called ASCII, which stands for the American Standard Code for Information Interchange. The decimal values of the characters in the ASCII set are shown in Table 1-2, where the ASCII code for any character is the sum of the numbers at the beginning of its row and column.

Although it is important to know that characters are represented internally using a numeric code, it is not generally useful to know what numeric value corresponds T A B L E 1 - 2 ASCII character codes

0 1 2 3 4 5 6 7 8 9 0x 1x 2x 3x 4x 5x 6x 7x 8x 9x 10x 11x 12x \000 \001 \002 \003 \004 \005 \006 \a \b \t \n \v \f \r \016 \017 \020 \021 \022 \023 \024 \025 \026 \027 \030 \031 \032 \033 \034 \035 \036 \037 space ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ \177

to a particular character. When you type the letter A, the hardware logic built into the keyboard automatically translates that character into the ASCII code 65, which is then sent to the computer. Similarly, when the computer sends the ASCII code 65 to the screen, the letter A appears.

You can write a character constant in C++ by enclosing the character in single quotes. Thus, the constant 'A' represents the internal code of the uppercase letter A. In addition to the standard characters, C++ allows you to write special characters in a multicharacter form beginning with a backward slash (\). This form is called an escape sequence. Table 1-3 shows the escape sequences that C++ supports.

Strings

Characters are most useful when they are collected together into sequential units. In programming, a sequence of characters is called a string. So far, the strings you’ve seen in the HelloWorld and PowersOfTwo programs have been used simply to display messages on the screen, but they have many more applications than that.

You write string constants in C++ by enclosing the characters contained within the string in double quotes. As with character, C++ uses the escape sequences from Table 1-3 to represent special characters. If two or more string constants appear consecutively in a program, the compiler concatenates them together. The most important implication of this rule is that you can break a long string over several lines so that it doesn’t end up running past the right margin of your program.

T A B L E 1 - 3 Escape sequences

\a Audible alert (beeps or rings a bell) \b Backspace

\f Formfeed (starts a new page)

\n Newline (moves to the beginning of the next line)

\r Return (returns to the beginning of the current line without advancing)

\t Tab (moves horizontally to the next tab stop)

\v Vertical tab (moves vertically to the next tab stop)

\0 Null character (the character whose ASCII code is 0)

\\ The character \ itself

\' The character ' (requires the backslash only in character constants)

\" The character " (requires the backslash only in string constants)

Given that they are essential to so many applications, all modern programming languages include special features for working with strings. Unfortunately, C++ complicates the issue by defining two different string types: an older style inherited from C and a more sophisticated string library that supports the object-oriented paradigm. To minimize confusion, this text uses the string library wherever possible, and you should—for the most part—feel free to ignore the fact that two string models exist. The times when that complexity raises its ugly head are outlined in Chapter 3, which covers the string library in more detail. For the moment, you can simply imagine that C++ offers a built-in data type called string whose domain is the set of all sequences of characters. You can declare variables of type string and pass string values back and forth between functions as arguments and results.

The fact that string is a library type and not a built-in feature does have a few implications. If you use the type name string in a program, you need to add the string library to the list of #include lines, like this:

#include <string>

Moreover, because the string type is part of the standard library namespace, the compiler will recognize the type name only if you have included the line

using namespace std;

at the beginning of the file, as the programs in this book invariably do.

Enumerated types

As the discussion of ASCII codes in the preceding section makes clear, computers store character data in integer by assigning a number to each character. This idea of encoding data as integers by numbering the elements of the domain is actually a much more general principle. C++ allows you to define new types simply by listing the elements in their domain. Such types are called enumerated types.

The syntax for defining an enumerated type is enum typename { namelist };

where typename is the name of the new type and namelist is a list of the constants in the domain, separated by commas. In this book, all type names start with an uppercase letter, and the names of the enumeration constants are written entirely in uppercase. For example, the following definition introduces a new Direction type whose values are the four compass directions:

When the C++ compiler encounters this definition, it assigns values to the constant names by numbering them consecutively starting with 0. Thus, NORTH is assigned the value 0, EAST is assigned the value 1, SOUTH is assigned the value 2, and WEST is assigned the value 3.

C++ allows you to assign explicit underlying values to each of the constants of an enumerated type. For example, the type declaration

enum Coin { PENNY = 1, NICKEL = 5, DIME = 10, QUARTER = 25, HALF_DOLLAR = 50, DOLLAR = 100 };

introduces an enumerated type for U.S. coinage in which each constant is defined to equal the monetary value of that coin. If you supply values for some of the constants but not others, the C++ compiler will automatically choose values for the unassigned constants by numbering them consecutively after the last value you supplied. Thus, the type declaration

enum Month { JANUARY = 1, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER };

introduces a type for the months of the year in which JANUARY has the value 1, FEBRUARY has the value 2, and so forth up to DECEMBER, which has the value 12.

Compound types

The atomic types described in the preceding sections form the basis of a very rich type system that allows you to create new types from existing ones. Moreover, because C++ represents a synthesis of the object-oriented and procedural

paradigms, the type system includes both objects and more traditional structures. Learning how to define and manipulate these types is, to a large extent, the theme of this entire book. It therefore does not make sense to squeeze a complete description of these types into Chapter 1. That’s what the rest of the chapters are for.

Over the years of teaching this material at Stanford, we have discovered that you are much more likely to master the concepts of object-oriented programming if the details of defining classes and objects are presented after you have had a chance to use them in a high-level way. This book adopts that strategy and postpones any discussion of how to create your own objects until Chapter 6, at which point you will have had plenty of time to discover just how useful objects can be.