2

C++ Syntax and Semantics, and the Program Development Process

images To understand how a C++ program is composed of one or more subprograms (functions).

images To know what a metalanguage is, and how it is used.

images To understand the concept of a data type.

images To learn the steps involved in entering and running a program.

To be able to:

images Read syntax templates so as to understand the formal rules governing C++ programs.

images Create and recognize legal C++ identifiers.

images Declare named constants and variables of type char and string.

images Distinguish reserved words in C++ from user-defined identifiers.

images Assign values to variables.

images Construct simple string expressions made up of constants, variables, and the concatenation operator.

images Construct a statement that writes to an output stream.

images Determine what is printed by a given output statement.

images Use comments to clarify your programs.

images Construct simple C++ programs.

2.1 The Elements of C++ Programs

Programmers develop solutions to problems using a programming language. In this chapter, we start looking at the rules and symbols that make up the C++ programming language. We also review the steps required to create a program and make it work on a computer.

C++ Program Structure

In Chapter 1, we talked about the four basic structures for expressing actions in a programming language: sequence, selection, loop, and subprogram. We said that subprograms allow us to write parts of our program separately and then assemble them into final form. In C++, all subprograms are referred to as functions, and a C++ program is a collection of one or more functions.

Each function performs some particular task, and collectively they all cooperate to solve the entire problem.

images

Every C++ program must have a function named main. Execution of the program always begins with the main function. You can think of main as the master and the other functions as the servants. When main wants the function Square to perform a task, main calls (or invokes) Square. When the Square function completes execution of its statements, it obediently returns control to the master, main, so the master can continue executing.

Let's look at an example of a C++ program with three functions: main, Square, and Cube. Don't be too concerned with the details in the program—just observe its overall look and structure.

images

In each of the three functions, the left brace ({) and the right brace ( }) mark the beginning and the end of the statements to be executed. Statements appearing between the braces are known as the body of the function.

Execution of a program always begins with the first statement of the main function. In our program, the first statement is

 cout << “ The square of 27 is” << Square(27) << endl;

This output statement causes information to be printed on the computer's display screen. You will learn how to construct output statements later in the chapter. Briefly, this statement prints two items. The first is the message

 The square of 27 is

The second item to be printed is the value obtained by calling (invoking) the Square function, with the value 27 as the number to be squared. As the servant, the Square function performs its task of squaring the number and sending the computed result (729) back to its caller, the main function. Now main can continue executing by printing the value 729 and proceeding to its next statement.

In a similar fashion, the second statement in main prints the message

and the cube of 27 is

and then invokes the Cube function and prints the result, 19683. The complete output produced by executing this program is, therefore,

images

Both Square and Cube are examples of value-returning functions. A value-returning function returns a single value to its caller. The word int at the beginning of the first line of the Square function

int Square( int n )

states that the function returns an integer value.

Now look at the main function again. You'll see that the first line of the function is

int main()

The word int indicates that main is a value-returning function that should return an integer value. And it does: After printing the square and cube of 27, main executes the statement

return 0;

to return the value 0 to its caller. But who calls the main function? The answer: the computer's operating system.

When you work with C++ programs, the operating system is considered to be the caller of the main function. The operating system expects main to return a value when main finishes executing. By convention, a return value of 0 means everything went okay. A return value of anything else (typically 1, 2,…) means something went wrong. Later in this book we look at situations in which you might want to return a value other than 0 from main. For the time being, we always conclude the execution of main by returning the value 0.

We have looked only briefly at the overall picture of what a C++ program looks like—a collection of one or more functions, including main. We have also mentioned what is special about the main function—it is a required function, execution begins there, and it returns a value to the operating system. Now it's time to begin looking at the details of the C++ language.

Syntax and Semantics

A programming language is a set of rules, symbols, and special words used to construct a program. In particular, there are rules for both syntax (grammar) and semantics (meaning).

Syntax is a formal set of rules that defines exactly which combinations of letters, numbers, and symbols can be used in a programming language. There is no room for ambiguity in the syntax of a programming language because the computer can't think; it doesn't “know what we mean.” To avoid ambiguity, syntax rules themselves must be written in a very simple, precise, formal language called a metalanguage.

Learning to read a metalanguage is like learning to read the notations used in the rules of a sport. Once you understand the notations, you can read the rule book. It's true that many people learn a sport simply by watching others play, but what they learn is usually just enough to allow them to take part in casual games. You could learn C++ by following the examples in this book, but a serious programmer, like a serious athlete, must take the time to read and understand the rules.

Syntax rules are the blueprints we use to build instructions in a program. They allow us to take the elements of a programming language—the basic building blocks of the language—and assemble them into constructs, which are syntactically correct structures. If our program violates any of the rules of the language—by misspelling a crucial word or leaving out an important comma, for instance—the program is said to have syntax errors and cannot compile correctly until we fix them.

images

Syntax Templates

In this book, we write the syntax rules for C++ using a metalanguage called a syntax template. A syntax template is a generic example of the C++ construct being defined. Graphic conventions show which portions are optional and which portions can be repeated. A boldface word or symbol is a literal word or symbol in the C++ language. A nonboldface word can be replaced by another template. A curly brace is used to indicate a list of items, from which one item can be chosen.

Let's look at an example. This template defines an identifier in C++:

images

The shading indicates a part of the definition that is optional. The three dots (…) mean that the preceding symbol or shaded block can be repeated. Thus an identifier in C++ must begin with a letter or underscore and is optionally followed by one or more letters, underscores, or digits.

Remember that a word not in boldface type can be replaced with another template. Here are the templates for Letter and Digit:

images

In these templates, the braces again indicate lists of items from which any one item can be chosen. So a letter can be any one of the uppercase or lowercase letters, and a digit can be any of the numeric characters 0 through 9.

Now let's look at the syntax template for the C++ main function:

images

The main function begins with the word int, followed by the word main and then left and right parentheses. This first line of the function is the heading. After the heading, the left brace signals the start of the statements in the function (its body). The shading and the three dots indicate that the function body consists of zero or more statements. (In this diagram we have placed the three dots vertically to suggest that statements usually are arranged vertically, one above the next.) Finally, the right brace indicates the end of the function.

In principle, the syntax template allows the function body to have no statements at all. In practice, the body should include a return statement because the word int in the function heading states that main returns an integer value. Thus the shortest C++ program possible is

images

As you might guess, this program does absolutely nothing useful when executed!

As we introduce C++ language constructs throughout the book, we use syntax templates to display the proper syntax. When you finish this chapter, you should know enough about the syntax and semantics of statements in C++ to write simple programs. But before we can talk about writing statements, we must look at how names are written in C++ and at some of the elements of a program.

Naming Program Elements: Identifiers

As we noted in our discussion of metalanguages, identifiers are used in C++ to name things—things such as subprograms and places in the computer's memory. Identifiers are made up of letters (A-Z, a-z), digits (0-9), and the underscore character ( _ ), but must begin with a letter or underscore.

Remember that an identifier must start with a letter or underscore:

images

(Identifiers beginning with an underscore have special meanings in some C++ systems, so it is best to begin an identifier with a letter.) Here are some examples of valid identifiers:

sum_of_squares J9 box_22A GetData Bin3D4 count

And here are some examples of invalid identifiers and the reasons why they are invalid:
 

Invalid Identifier

Explanation

40Hours

Identifiers cannot begin with a digit.

Get Data

Blanks are not allowed in identifiers.

box-22

The hyphen (-) is a math symbol (minus) in C++.

cost_in_$

Special symbols such as $ are not allowed.

int

The word int is predefined in the C++ language.

The last identifier in the table, int, is an example of a reserved word. Reserved words are words that have specific uses in C++; you cannot use them as programmer-defined identifiers. Appendix A lists all of the reserved words in C++.

The LeapYear program in Chapter 1 uses the programmer-defined identifiers in the following list. (Most of the other identifiers in the program are C++ reserved words.) Notice that we chose the names to convey how the identifiers are used.
 

Identifier

How It Is Used

year

Year to be tested

IsLeapYear

Whether year is a leap year

images

Now that we've seen how to write identifiers, let's look at some of the things that C++ allows us to name.

Data and Data Types

A computer program operates on data (stored internally in memory, stored externally on disk, or input from a device such as a keyboard, scanner, network, or electrical sensor) and produces output. In C++, each piece of data must be of a specific data type. The data type determines how the data is represented in the computer and the kinds of processing the computer can perform on it.

Some types of data are used so frequently that C++ defines them for us. Examples of these standard (or built-in) types include int (for working with integer numbers), float (for working with real numbers having decimal points), and char (for working with character data).

Additionally, C++ allows programmers to define their own data types—known as programmer-defined (or user-defined) types. Beginning in Chapter 10, we show you how to define your own data types.

In this chapter, we focus on two data types—one for representing data consisting of a single character, the other for representing strings of characters. In Chapter 3, we examine the numeric types (such as int and float) in detail.

The char Data Type

The built-in type char describes data consisting of one alphanumeric character—a letter, a digit, or a special symbol:

‘A’ ‘a’ ‘8’ ‘2’ ‘+’ ‘-’ ‘$’ ‘?’ ‘*’ ‘ ’

Each machine uses a particular character set, the set of alphanumeric characters it can represent. (See Appendix E for some sample character sets.) Notice that each character is enclosed in single quotes (apostrophes). The C++ compiler needs the quotes to differentiate, say, between the character data '8' and the integer value 8 because the two are stored differently inside the machine. Notice also that the blank, ' ', is a valid character 1.

You wouldn't want to add the character 'A' to the character 'B' or subtract the character '3' from the character '8', but you might want to compare character values. Each character set has a collating sequence, a predefined ordering of all the characters. Although this sequence varies from one character set to another, 'A' always compares less than 'B', 'B' less than 'C', and so forth. Also, '1' compares less than '2', '2' less than '3', and so on. None of the identifiers in the LeapYear program is of type char.

The string Data Type

Whereas a value of type char is limited to a single character, a string is a sequence of characters, such as a word, name, or sentence, enclosed in double quotes. For example, the following are strings in C++:

“Problem Solving” “C++”  “Programming and”  “ . ”

A string must be typed entirely on one line. For example, the string

“This string is invalid because it is typed on more than one line.”

is not valid because it is split across two lines. In this situation, the C++ compiler issues an error message at the first line. The message may say something like UNTERMINATED STRING, depending on the particular compiler.

The quotes are not considered to be part of the string but are simply there to distinguish the string from other parts of a C++ program. For example, “amount” (in double quotes) is the character string made up of the letters a, m, o, u, n, and t in that order. By contrast, amount (without the quotes) is an identifier, perhaps the name of a place in memory. The symbols “12345” represent a string made up of the characters 1, 2, 3, 4, and 5 in that order. If we write 12345 without the quotes, it is an integer quantity that can be used in calculations.

A string containing no characters is called the null string (or empty string). We write the null string using two double quotes with nothing (not even spaces) between them:

“”

The null string is not equivalent to a string of spaces; it is a special string that contains no characters.

To work with string data, this book uses a data type named string. This data type is not part of the C++ language (that is, it is not a built-in type). Rather, string is a programmer-defined type that is supplied by the C++ standard library, a large collection of prewritten functions and data types that any C++ programmer can use. Operations on string data include comparing the values of strings, searching a string for a particular character, and joining one string to another. We look at some of these operations later in this chapter and cover additional string operations in subsequent chapters. None of the identifiers in the LeapYear program is of type string, although string values are used directly in several places in the program.

Naming Elements: Declarations

Identifiers can be used to name both constants and variables. In other words, an identifier can be the name of a memory location whose contents are not allowed to change or it can be the name of a memory location whose contents can change.

How do we tell the computer what an identifier represents? By using a declaration, a statement that associates a name (an identifier) with a description of an element in a C++ program (just as a dictionary definition associates a name with a description of the thing being named). In a declaration, we name an identifier and indicate what it represents. For example, the LeapYear program uses the declaration

int year;

to announce that year is the name of a variable whose contents are of type int. When we declare a variable, the compiler picks a location in memory to be associated with the identifier. We don't have to know the actual address of the memory location because the computer automatically keeps track of it for us.

Suppose that when we mailed a letter, we only had to put a person's name on it and the post office would look up the address. Of course, everybody in the world would need a different name; otherwise, the post office wouldn't be able to figure out whose address was whose. The same is true in C++. Each identifier can represent just one thing (except under special circumstances, which we talk about in Chapters 7 and 8). Every identifier you use in a program must be different from all others.

Constants and variables are collectively called data objects. Both data objects and the actual instructions in a program are stored in various memory locations. You have seen that a group of instructions—a function—can be given a name. A name also can be associated with a programmer-defined data type.

In C++, you must declare every identifier before it is used. This practice allows the compiler to verify that the use of the identifier is consistent with what it was declared to be. If you declare an identifier to be a constant and later try to change its value, for example, the compiler detects this inconsistency and issues an error message.

There is a different form of declaration statement for each kind of data object, function, or data type in C++. The forms of declarations for variables and constants are introduced here; others are covered in later chapters.

Variables

A program operates on data, which is stored in memory. While a program is executing, different values may be stored in the same memory location at different times. This kind of memory location is called a variable, and its content is the variable value. The symbolic name that we associate with a memory location is the variable name or variable identifier (see FIGURE 2.1). In practice, we often refer to the variable name more briefly as the variable.

Declaring a variable means specifying both the variable's name and its data type. The declaration tells the compiler to associate a name with a memory location whose contents are of a specific type (for example, char or string). The following statement declares myChar to be a variable of type char:

char myChar;

images

Figure 2.1 Variable

In C++, a variable can contain a data value only of the type specified in its declaration. Because of the preceding declaration, the variable myChar can contain only a char value. If the C++ compiler comes across an instruction that tries to store a float value into myChar, it generates extra instructions to convert the float value to the proper type. In Chapter 3, we examine how such type conversions take place.

Here's the syntax template for a variable declaration:
 

images

where DataType is the name of a data type such as char or string. Notice that a variable declaration always ends with a semicolon.

From the syntax template, you can see that it is possible to declare several variables in one statement:

char letter, middleInitial, ch;

Here, all three variables are declared to be char variables. Our preference, though, is to declare each variable with a separate statement:

char letter;
char middleInitial;
char ch;

This form makes it easier, when modifying a program, to add new variables to the list or delete ones you no longer want.

Declaring each variable with a separate statement also allows you to attach comments to the right of each declaration, as shown here:

images

These declarations tell the compiler to reserve memory space for three float variables— payRate, hours, and wages—and one int variable, empNum. The comments explain to someone reading the program what each variable represents.

Now that we've seen how to declare variables in C++, let's look at how to declare constants.

Constants

All single characters (enclosed in single quotes) and strings (enclosed in double quotes) are constants.

‘A’ ‘@’ “Howdy boys” “Please enter an employee number:”

In C++, as in mathematics, a constant is something whose value never changes. When we use the actual value of a constant in a program, we are using a literal value (or literal).

An alternative to the literal constant is the named constant (or symbolic constant), which is introduced in a declaration statement. A named constant is just another way of representing a literal value. Instead of using the literal value in an instruction, we give it a name in a declaration statement, then use that name in the instruction. For example, we can write an instruction that prints the title of this book using the literal string “Programming and Problem Solving with C++”. Alternatively, we can declare a named constant called BOOK_TITLE that equals the same string and then use the constant name in the instruction. That is, we can use either

“Programming and Problem Solving with C++”

or

BOOK_TITLE

in the instruction.

Using a literal value may seem easier than declaring a named constant and then referring to it. But, in fact, named constants make a program easier to read because they make the meaning clearer. Named constants also make it easier to change a program at a later time.

Here is the syntax template for a constant declaration:

images

Notice that the reserved word const begins the declaration, and an equal sign (=) appears between the identifier and the literal value.

The following are examples of constant declarations:

images

As we have done above, many C++ programmers capitalize the entire identifier of a named constant and separate the English words with an underscore. The idea is to let the reader quickly distinguish between variable names and constant names when they appear in the middle of a program.

It's a good idea to add comments to constant declarations as well as variable declarations. We describe in comments what each constant represents:

const float MAX_HOURS = 40.0; // Maximum normal work hours
const float OVERTIME = 1.5;   // Overtime pay rate factor

Here is a program that contains just declarations:

images

images

Taking Action: Executable Statements

Up to this point, we've looked at ways of declaring data objects in a program. Now we turn our attention to ways of acting, or performing operations, on data.

Assignment

The value of a variable can be set or changed through an assignment statement. For example,

lastName = “Lincoln”;

assigns the string value “Lincoln” to the variable lastName; that is, it stores the sequence of characters “Lincoln” into the memory associated with the variable named lastName.

Here's the syntax template for an assignment statement:
 

images

The semantics (meaning) of the assignment operator (=) is “store”; the value of the expressionis stored into the variable. Any previous value in the variable is destroyed and replaced by the value of the expression.

Only one variable can be on the left-hand side of an assignment statement. An assignment statement is not like a math equation (x + y = z + 4); the expression (what is on the right-hand side of the assignment operator) is evaluated, and the resulting value is stored into the single variable on the left of the assignment operator. A variable keeps its assigned value until another statement stores a new value into it.

Given the declarations

string firstName;
string middleName;
string lastName;
string title;
char middleInitial;
char letter;

the following assignment statements are valid:

firstName = “Abraham”;
middleName = firstName;
middleName = “”;
lastName = “Lincoln”;
title = “President”;
middleInitial = ' ';
letter = middleInitial;

However, these assignments are not valid:
 

Invalid Assignment Statement

Reason

middleInitial = “A.”;

middleInitial is of type char; “A.” is of type string.

letter = firstName;

letter is of type char; firstName is of type string.

firstName = Thomas;

Thomas is an undeclared identifier.

“Edison” = lastName;

Only a variable can appear to the left of =.

lastName = ;

The expression to the right of = is missing.

String Expressions

Although we can't perform arithmetic on strings, the string data type provides a special string operation, called concatenation, that uses the + operator. The result of concatenating (joining) two strings is a new string containing the characters from both strings. For example, given the statements

string bookTitle;
string phrase1;
string phrase2;
phrase1 = “Programming and “;
phrase2 = “Problem Solving”;

we could write

bookTitle = phrase1 + phrase2;

This statement retrieves the value of phrase1 from memory and concatenates the value of phrase2 to form a new, temporary string containing the characters

“Programming and Problem Solving”

This temporary string (which is of type string) is then assigned to (stored into) book-Title.

The order of the strings in the expression determines how they appear in the resulting string. If we write

bookTitle = phrase2 + phrase1;

then bookTitle contains

“Problem SolvingProgramming and “

Concatenation works with named string constants, literal strings, and char data, as well as with string variables. The only restriction is that at least one of the operands of the + operator must be a string variable or named constant (so you cannot use expressions like “Hi” + “there” or ‘A’ + ‘B’). For example, if we have declared the following constants

const string WORD1 = “Programming”;
const string WORD3 = “Solving”;
const string WORD5 = “C++”;

then we could write the following assignment statement to store the title of this book into the variable bookTitle:

bookTitle = ‘P’ + WORD1 + “ and Problem “ + WORD3 + “ with “ + WORD5;

As a result, bookTitle contains the string

“Programming and Problem Solving with C++”

The preceding example demonstrates how we can combine identifiers, char data, and literal strings in a concatenation expression. Of course, if we simply want to assign the complete string to bookTitle, we can do so directly:

bookTitle = “Programming and Problem Solving with C++”;

Occasionally we may encounter a situation in which we want to add some characters to an existing string value. Suppose that bookTitle already contains “Programming and Problem Solving” and now we wish to complete the title. We could use a statement of the form

bookTitle = bookTitle + “ with C++”;

Such a statement retrieves the value of bookTitle from memory, concatenates the string “with C++” to form a new string, and then stores the new string back into bookTitle. The new string replaces the old value of bookTitle (which is destroyed).

Keep in mind that concatenation works only with values of type string. Even though an arithmetic plus sign is used for the operation, we cannot concatenate values of numeric data types, such as int and float, with strings.

Here is a program that shows how declarations and assignment statements could be arranged with respect to one another.

images

Output

Have you ever asked someone, “Do you know what time it is?” only to have the person smile smugly, say, “Yes, I do,” and walk away? This situation is like the one that currently exists between you and the computer. You now know enough C++ syntax to tell the computer to assign values to variables and to concatenate strings, but the computer won't give you the results until you tell it to write them out.

In C++, we write out the values of variables and expressions by using a special variable named cout (pronounced “see-out”) along with the insertion operator (<<):

cout << “Hello”;

This statement displays the characters Hello on the standard output device, usually the display screen.

The variable cout is predefined in C++ systems to denote an output stream. You can think of an output stream variable as a doorway through which we send a sequence of characters to an output device.

The insertion operator << (often pronounced as “put to”) takes two operands. Its lefthand operand is a stream expression (in the simplest case, just a stream variable such as cout). Its right-hand operand is an expression, as in the following two examples:

cout << “The title is “;
cout << bookTitle + “, 5th Edition”;

The insertion operator converts its right-hand operand to a sequence of characters and sends them to the output device through the stream variable.2 Notice how the << operator points in the direction the data is going—from the expression written on the right to the output stream on the left.

You can use the << operator several times in a single output statement. Each occurrence appends the next data item to the output stream. For example, we can write the preceding two output statements as

cout << “The title is “ << bookTitle + “, 5th Edition”;

If bookTitle contains “American History“, both versions produce the same output:

The title is American History, 5th Edition

The output statement has the following form:

images

The following output statements yield the output shown. These examples assume that the char variable ch contains the value '2', the string variable firstName contains “Marie“, and the string variable lastName contains “Curie“.
 

Statement

What Is Printed (images means blank)

cout << ch;

2

cout << “ch = “ << ch;

chimages=images2

cout << firstName + “ ” + lastName;

MarieimagesCurie

cout << firstName << lastName;

MarieCurie

cout << firstName << ' ' << lastName;

MarieimagesCurie

cout << “ERROR MESSAGE”;

ERRORimagesMESSAGE

cout << “Error=” << ch;

Error=2

An output statement prints literal strings exactly as they appear. To let the computer know that you want to print a literal string—and not a named constant or variable—you must remember to use double quotes to enclose the string. If you don't put quotes around a string, you'll probably get an error message (such as UNDECLARED IDENTIFIER) from the C++ compiler. If you want to print a string that includes a double quote, you must type a backslash () character and a double quote, with no space between them, in the string. For example, to print the characters

Al “Butch” Jones

the output statement would look like this:

cout << “Al ”Butch“  Jones”;

To conclude this introductory look at C++ output, we should mention how to terminate an output line. Normally, successive output statements cause the output to continue along the same line of the display screen. For example, the sequence

cout << “Hi”;
cout << “there”;

writes the following to the screen, all on the same line:

Hithere

To print the two words on separate lines, we can write this:

cout << “Hi”  << endl;
cout << “there”  << endl;

The output from these statements is

Hi
There

The identifier endl (meaning “end line”) is a special C++ feature called a manipulator. We discuss manipulators in detail in Chapter 3. For now, the important thing to note is that endl lets you finish an output line and go on to the next line whenever you wish.

Here is a program that contains declarations, assignment statements, and output statements.

images

images

Beyond Minimalism: Adding Comments to a Program

All you need to create a working program is the correct combination of declarations and executable statements. The compiler ignores comments, but they are of enormous help to anyone who must read the program. Comments can appear anywhere in a program except in the middle of an identifier, a reserved word, or a literal constant.

C++ comments come in two forms. The first is any sequence of characters enclosed by the /* */ pair. The compiler ignores anything within the pair. Here's an example:

string idNumber; /* Identification number of the aircraft */

The second, and more common, form begins with two slashes (//) and extends to the end of that line of the program:

string idNumber; // Identification number of the aircraft

The compiler ignores anything after the two slashes.

Writing fully commented programs is good programming style. A comment should appear at the beginning of a program to explain what the program does:

// This program computes the weight and balance of a Beechcraft
// Starship-1 airplane, given the amount of fuel, number of
// passengers, and weight of luggage in fore and aft storage.
// It assumes that there are two pilots and a standard complement
// of equipment, and that passengers weigh 170 pounds each.

Another good place for comments is in constant and variable declarations, where the comments explain how each identifier is used. In addition, comments should introduce each major step in a long program and should explain anything that is unusual or difficult to read (for example, a lengthy formula).

It is important to make your comments concise and to arrange them in the program so that they are easy to see and it is clear what they refer to. If comments are too long or crowd the statements in the program, they make the program more difficult to read—just the opposite of what you intended!

2.2 Program Construction

We have looked at basic elements of C++ programs: identifiers, declarations, variables, constants, expressions, statements, and comments. Now let's see how to collect these elements into a program. As you saw earlier, C++ programs are made up of functions, one of which must be named main. A program also can have declarations that lie outside of any function. The syntax template for a program looks like this:

images

A function definition consists of the function heading and its body, which is delimited by left and right braces:

images

Here's an example of a program with just one function, the main function:

images

The program begins with a comment that explains what the program does. Immediately after the comment, the following lines appear:

#include <iostream>
#include <string>
using namespace std;

The #include lines instruct the C++ system to insert into our program the contents of the files named iostream and string. The first file contains information that C++ needs to output values to a stream such as cout. The second file contains information about the programmer-defined data type string. We discuss the purpose of these #include lines and the using statement a little later in the chapter.

Next comes a declaration section in which we define the constants FIRST, LAST, and MIDDLE.3 Comments explain how each identifier is used. The rest of the program is the function definition for our main function. The first line is the function heading: the reserved word int, the name of the function, and then opening and closing parentheses. (The parentheses inform the compiler that main is the name of a function, and not a variable or named constant.) The body of the function includes the declarations of two variables, firstLast and lastFirst, followed by a list of executable statements. The compiler translates these executable statements into machine language instructions. During the execution phase of the program, these instructions will be executed.

Our main function finishes by returning 0 as the function value:

return 0;

Recall that main returns an integer value to the operating system when it completes execution. This integer value is called the exit status. On most computer systems, you return an exit status of 0 to indicate successful completion of the program; otherwise, you return a nonzero value. Here is the output from the program.

images

Notice how we use spacing in the PrintName program to make it easy for someone to read. We use blank lines to separate statements into related groups, and we indent the entire body of the main function. The compiler doesn't require us to format the program this way; we do so only to make it more readable. We will have more to say about formatting a program in Chapter 3.

Blocks (Compound Statements)

The body of a function is an example of a block (or compound statement). This is the syntax template for a block:

images

A block is just a sequence of zero or more statements enclosed (delimited) by a { } pair. Now we can redefine a function definition as a heading followed by a block:

images

In later chapters, when we learn how to write functions other than main, we will define the syntax of the heading in detail. In the case of the main function, the heading is simply

int main()

Here is the syntax template for a statement, limited to the C++ statements discussed in this chapter:

images

A statement can be empty (the null statement). The null statement is just a semicolon (;) and looks like this:

;

It does absolutely nothing at execution time; execution just proceeds to the next statement. The null statement is not used often.

As the syntax template shows, a statement can also be a declaration, an executable statement, or even a block. The last term means that you can use an entire block wherever a single statement is allowed. In later chapters in which we introduce the syntax for branching and looping structures, this fact is very important.

We use blocks often, especially as parts of other statements. Leaving out a { } pair can dramatically change the meaning as well as the execution of a program. This is why we always indent the statements inside a block—the indentation makes a block easy to spot in a long, complicated program.

Notice in the syntax templates for the block and the statement that there is no mention of semicolons—yet the PrintName program contains many semicolons. If you look back at the templates for constant declaration, variable declaration, assignment statement, and output statement, you can see that a semicolon is required at the end of each kind of statement. However, the syntax template for the block shows no semicolon after the right brace. The rule for using semicolons in C++, then, is quite simple: Terminate each statement except a compound statement (block) with a semicolon.

One more thing about blocks and statements: According to the syntax template for a statement, a declaration is officially considered to be a statement. A declaration, therefore, can appear wherever an executable statement can. In a block, we can mix declarations and executable statements if we wish:

{
  char ch;
  ch = ‘A’;
  cout << ch;
  string str;
  str = “Hello”;
  cout << str;
}

We prefer, however, to group the declarations together before the start of the executable statements:

{
  char   ch;
  string str;
  ch = ‘A’;
  cout << ch;
  str = “Hello”;
  cout << str;
}

The C++ Preprocessor

Imagine that you are the C++ compiler. You are presented with the following program. You are to check it for syntax errors and, if there are no syntax errors, you are to translate it into machine language code.

images

You, the compiler, recognize the identifier int as a C++ reserved word and the identifier main as the name of a required function. But what about the identifiers cout and endl? The programmer has not declared them as variables or named constants, and they are not reserved words. You have no choice but to issue an error message and give up.

To fix this program, the first thing we must do is insert a line near the top that says

#include <iostream>       // For cout, endl

just as we did in the PrintName program (as well as in the sample program at the beginning of this chapter and the LeapYear program of Chapter 1). This line says to insert the contents of a file named iostream into the program. This file contains declarations of cout, endl, and other items needed to perform stream input and output. The #include line is not handled by the C++ compiler, but rather by a program known as the preprocessor.

The preprocessor concept is fundamental to C++. The preprocessor is a program that acts as a filter during the compilation phase. Your source program passes through the preprocessor on its way to the compiler (see FIGURE 2.2).

images

Figure 2.2 C++ Preprocessor

A line beginning with a pound sign (#) is not considered to be a C++ language statement (and thus is not terminated by a semicolon). Instead, it is called a preprocessor directive. The preprocessor expands an #include directive by physically inserting the contents of the named file into your source program. A file whose name appears in an #include directive is called a header file. Header files contain constant, variable, data type, and function declarations needed by a program.

In the directives

#include <iostream>        // For cout, endl
#include <string>

the angle brackets < > are required. They tell the preprocessor to look for the files in the standard include directory—which contains all the header files that are related to the C++ standard library. The file iostream contains declarations of input/output facilities, and the file string contains declarations for the string data type. In Chapter 3, we make use of header files other than iostream and string.4

Using the std Namespace

In our HappyBirthday program, even if we add the preprocessor directive #include <iostream>, the program will not compile. The compiler still doesn't recognize the identifiers cout and endl. The problem is that the header file iostream (and, in fact, every standard header file) declares all of its identifiers to be in a namespace block called std:

namespace std
{      // Start of namespace block
  :    // Declarations of variables, data types, and so forth
}      // End of namespace block

Don't worry about writing namespace block declarations for now; the important point is that an identifier declared within a namespace block can be accessed directly only by statements within that same block. To access an identifier that is “hidden” inside a namespace, the programmer has several options. We describe the simplest one here. Chapter 8 describes namespaces in more detail.

If we insert a statement called a using directive

using namespace std;

near the top of the program before the main function, then we make all the identifiers in the std namespace accessible to our program.

images

This option is the one we used in the PrintName program and in sample programs earlier in the chapter. In many of the following chapters, we continue to use this method. However, in Chapter 8 we discuss why the approach is not advisable in large programs.

images

images

images

2.3 More about Output

We can control both the horizontal and vertical spacing of our output to make it more appealing (and understandable). Let's look first at vertical spacing.

Creating Blank Lines

We control vertical spacing by using the endl manipulator in an output statement. You have seen that a sequence of output statements continues to write characters across the current line until an endl terminates the line. Here are some examples:
 

Statements

Output Produced5

cout << “Hi there, ”;

cout << “Lois Lane. ” << endl;

Hi there, Lois Lane.

cout << “Have you seen ”;

cout << “Clark Kent?” << endl;

Have you seen Clark Kent?

cout << “Hi there, ” << endl;

Hi there,

cout << “Lois Lane. ” << endl;

Lois Lane.

cout << “Have you seen ” << endl;

Have you seen

cout << “Clark Kent?” << endl;

Clark Kent?

cout << “Hi there, ” << endl;

Hi there,

cout << “Lois Lane. ”;

cout << “Have you seen ” << endl;

Lois Lane. Have you seen

cout << “Clark Kent?” << endl;

Clark Kent?

What do you think the following statements print out?

cout << “Hi there, ” << endl;
cout << endl;
cout << “Lois Lane.”  << endl;

The first output statement causes the words Hi there, to be printed; the endl causes the screen cursor to go to the next line. The next statement prints nothing but goes on to the next line. The third statement prints the words Lois Lane. and terminates the line. The resulting output is the three lines

Hi there,
Lois Lane.

Whenever you use an endl immediately after another endl, a blank line is produced. As you might guess, three consecutive uses of endl produce two blank lines, four consecutive uses produce three blank lines, and so forth.

Note that we have a great deal of flexibility in how we write an output statement in a C++ program. We could combine the three preceding statements into two statements:

cout << “Hi there, “ << endl << endl;
cout << “Lois Lane.”  << endl;

In fact, we could do it all in one statement. One possibility is this:

cout << “Hi there, “ << endl << endl << “Lois Lane.”  << endl;

Here's another:

cout << “Hi there, “ << endl << endl
         << “Lois Lane.”  << endl;

The last example shows that you can spread a single C++ statement onto more than one line of the program. The compiler treats the semicolon—not the physical end of a line—as the end of a statement.

Inserting Blanks within a Line

To control the horizontal spacing of the output, one technique is to send extra blank characters to the output stream. (Remember that the blank character, which is generated by pressing the spacebar on a keyboard, is a perfectly valid character in C++.)

images

All of the blanks and asterisks are enclosed in double quotes, so they print literally as they are written in the program. The extra endl manipulators give you the blank lines between the rows of asterisks.

If you want blanks to be printed, you must enclose them in quotes. The statement

cout << ‘*’ <<             ‘*’;

produces the following output:

**

Despite all of the blanks we included in the output statement, the asterisks print side by side because the blanks are not enclosed by quotes.

Special Characters

We have already mentioned that to include a double-quote mark within a string, it must be preceded by a backslash (). C++ defines some additional special characters using the backslash prefix. Here we discuss three that are particularly useful.

We use the endl manipulator in an output stream to go to the next line. You can also use the newline character to place a newline operation directly in a string. The following two output statements both produce the same output:

cout << “The answer is:
42
”;
cout << “The answer is:”  << endl << “42”  << endl;

They each display:

The answer is:
42

The second statement is clearly more readable, of course. Starting with Chapter 8, we'll encounter cases where embedding in a string makes sense. For now, we recommend always using endl.

If you want to include a single quote mark as a char literal within an output stream, then it must also be preceded by a backslash: '''. Note that there is no problem including single quote marks in strings or writing a double quote directly as a char literal. For example, to output a double quote followed by a single quote, we can write either:

cout << '”' << “'”;      // Outputs “'

or

cout << “””  << ''';    // Also outputs “'

Given that the backslash has these special uses, it is natural to wonder how a backslash would be output. The answer is that we must write two backslashes in a row:

cout << “\”;           // Outputs 

2.4 Program Entry, Correction, and Execution

In this section, we examine the program entry process in general. You should consult the manual for your specific computer to learn the details.

Entering a Program

The first step in entering a program is to get the computer's attention. With a personal computer, you just turn it on or wake it from sleep mode. If you're using a computer in an educational lab, you may need to log on by entering a user name and password.

Once the computer is ready to accept your commands, you tell it that you want to enter a program by having it run a development environment, which includes a code editor. The code editor is a program that allows you to create and modify the code for a program, which is then stored on a file.

A file is a collection of data that has a name associated with it. You usually choose the name for the file when you create it with the editor. From that point on, you refer to the file by the name you've given it.

There are so many different types of editors, each with different features, that we can't begin to describe them all here. But we can describe some of their general characteristics.

A code editor is very much like a word processor. It allows you to type in a window and move around the file with your mouse and keyboard to make changes. In most development environments, the code editor also color-codes different parts of C++ syntax to make it easier to more distinctly see keywords, variables, comments, and so on. It will also help you to automatically indent lines of code. Some code editors check syntax as you type, flagging parts of your code that may not compile.

In many development environments, the compiler will automatically use the code editor to display the location where it detects an error in your code. Note, however, that a compiler sometimes detects an error in a place much different from where it actually occurred. For example, the compiler will point out the first place that an undeclared variable name is used, when the source of the error may be that you forgot a using statement.

Compiling and Running a Program

Once your program is stored in a file, you compile it by issuing a command to run the C++ compiler. The compiler translates the program, then stores the machine language version into a file. The compiler may display messages indicating errors in the program, either in a separate window or in the editor.

If the compiler finds errors in your program (syntax errors), you have to determine their cause, fix them in the editor, and then run the compiler again. Once your program compiles without errors, you can run (execute) it.

Some systems automatically run a program when it compiles successfully. On other systems, you have to issue a separate command to run the program. Still other systems require that you specify an extra step called linking between compiling and running a program. Whatever series of commands your system uses, the result is the same: Your program is loaded into memory and executed by the computer.

Even though a program runs, it may still have errors in its design. The computer does exactly what you tell it to do, even if that's not what you wanted it to do. If your program doesn't do what it should (a logic error), you have to return to the algorithm and fix it, and then go back to the editor and fix the program. Finally, you compile and run the program again. This debugging process is repeated until the program does what it is supposed to do (see FIGURE 2.3).

Figure 2.3 Debugging Process

images

images

images

images

images

Testing and Debugging

1. Every identifier that isn't a C++ reserved word must be declared. If you use a name that hasn't been declared—either by creating your own declaration statements or by including a header file—you get an error message.

2. If you try to declare an identifier that is the same as a reserved word in C++, you get an error message from the compiler. See Appendix A for a list of reserved words.

3. C++ is a case-sensitive language. Two identifiers that are capitalized differently are treated as two different identifiers. The word main and all C++ reserved words use only lowercase letters.

4. To use identifiers from the standard library, such as cout and string, you must put a using directive near the top of your program:

using namespace std;

5. Check for mismatched quotes in char and string literals. Each char literal begins and ends with an apostrophe (a single quote). Each string literal begins and ends with a double quote.

6. Be sure to use only the apostrophe (') to enclose char literals. Most keyboards also have a reverse apostrophe ('), which is easily confused with the apostrophe. If you use the reverse apostrophe, the compiler will issue an error message.

7. To use a double quote within a literal string, use the two symbols in a row. If you use just a double quote, it ends the string, and the compiler then sees the remainder of the string as an error.

8. In an assignment statement, be sure that the identifier to the left of = is a variable and not a named constant.

9. In assigning a value to a string variable, the expression to the right of = must be a string expression, a string literal, or a char.

10. In a concatenation expression, at least one of the two operands of + must be of type string. For example, the operands cannot both be literal strings or char values.6

11. Make sure your statements end in semicolons (except blocks, which do not have a semicolon after the right brace).

images    Summary

The syntax (grammar) of the C++ language is defined by a metalanguage. In this text, we use a form of metalanguage called syntax templates. We describe the semantics (meaning) of C++ statements in English.

Identifiers are used in C++ to name things. Some identifiers, called reserved words, have predefined meanings in the language; others are created by the programmer. The identifiers you invent are restricted to those not reserved by the C++ language. Reserved words are listed in Appendix A.

Identifiers are associated with memory locations by declarations. A declaration may give a name to a location whose value does not change (a constant) or to one whose value can change (a variable). Every constant and variable has an associated data type. C++ provides many built-in data types; the most commonly used are int, float, and char. Additionally, C++ permits programmer-defined types, such as the string type from the standard library.

The assignment operator is used to change the value of a variable by assigning it the value of an expression. At execution time, the expression is evaluated and the result is stored into the variable. With the string type, the plus sign (+) is an operator that concatenates two strings. A string expression can concatenate any number of strings to form a new string value.

Program output is accomplished by means of the output stream variable cout, along with the insertion operator (<<). Each insertion operation sends output data to the standard output device. When an endl manipulator appears instead of a data item, the computer terminates the current output line and goes on to the next line.

Output should be clear, understandable, and neatly arranged. Messages in the output should describe the significance of values. Blank lines (produced by successive uses of the endl manipulator) and blank spaces within lines help to organize the output and improve its appearance.

A C++ program is a collection of one or more function definitions (and optionally some declarations outside of any function). One of the functions must be named main. Execution of a program always begins with the main function. Collectively, the functions all cooperate to produce the desired results.

images    Quick Check

1. What is the name of the one function that every C++ program must have? (p. 44)

2. What is the purpose of a metalanguage? (p. 47)

3. What is a data type? (p. 50)

4. If you discover a logic error in your program, do you go directly to the editor and start changing the code? (pp. 75–76)

5. Use the following syntax template to decide if the string “C++ 5th edition” is a valid “Sentence.” (pp. 47–48)

images

6. Which of the following are valid C++ identifiers? (pp. 49–50)

Hello  Bob   5th-edition   C++   maximum all_4_one

7. The only difference between a variable declaration and a constant declaration is that the reserved word const precedes the declaration. True or false? (pp. 55–56)

8. The reserved words num-listed in Appendix A cannot be used as identifiers. True or false? (p. 49)

9. Write a statement to assign the letter “A” to the char variable initial. (pp. 57–60)

10. What value is stored in the variable name by the following statement? (pp. 57–60)

name = “Alexander”  + “ Q. “ + “Smith”;

11. Write a statement that sends the value in variable name to the stream cout. (pp. 60- 62)

12. What is printed by the following statement, given the value assigned to name in Exercise 10? (pp. 60–62)

cout << name << “ Jr.”  << endl;

13. What are the two ways to write comments in C++? (pp. 62–63)

14. Fill in the blanks in the following statements, which appear at the beginning of a program. (pp. 69–71)

#include <_________>
#include <_________>
using namespace _______;

images    Answers

1. main 2. To provide a language for expressing the syntax rules of a programming language. 3. A specific set of values, together with a set of operations that can be applied to those values. 4. No. You go back to the algorithm, correct the general solution to the problem, and then translate the correction into code before you use the editor to change your program. 5. No. According to the template, a Sentence must end in a period, exclamation point, or question mark. 6. Hello, Bob, maximum, and all_4_one are the valid C++ identifiers. 7. False. The constant declaration also assigns a literal value to the identifier. 8. True. Identifiers must differ from reserved words. 9. initial = ‘A’; 10. Alexander Q. Smith 11. cout << name; 12. The string Alexander Q. Smith Jr. is output and then successive output will appear on the next line. 13. Between the delimiters /* and */, or following // to the end of the line.

14. #include <iostream>
    #include <string>
    using namespace std;

images    Exam Preparation Exercises

1. Mark the following identifiers as either valid or invalid.

images

2. Match the following terms with the definitions given below.

a. Function

b. Syntax

c. Semantics

d. Metalanguage

e. Identifier

f. Data type

g. Variable

h. Named constant

i. Literal

j. Expression

i. An identifier referring to a value that can be changed.

ii. A specific set of values, along with operations that can be applied to those values.

iii. A value that appears in a program.

iv. The set of rules that determines the meaning of instructions written in a programming language.

v. A language that is used to write the rules for another language.

vi. A subprogram in C++.

vii. A name that is used to refer to a function or data object.

viii.An arrangement of identifiers, literals, and operators that can be evaluated.

ix. The formal rules governing how valid instructions are written in a programming language.

x. An identifier referring to a value that cannot be changed.

3. A reserved word is a named constant that is predefined in C++. True or false?

4. What is wrong with the following syntax template for a C++ identifier?

images

5. A char literal can be enclosed either in single or double quotes. True or false?

6. The null string represents a string containing no characters. True or false?

7. Concatenation works with a char value only when its other operand is a string value. True or false?

8. What is output by the following statement?

images

9. Given these assignments to string variables:

images

What is the value of the following expression?

images

10. How do we insert a double quote into a string?

11. What danger is associated with the use of the /* and */ form of comment, and is avoided by use of the // comment form?

12. What are the limitations associated with the // form of comment?

13. What is the name of the << operator, and how might we pronounce it in reading a line of code that contains it?

14. Can we concatenate endl with a string in an expression? Explain.

15. What does the #include preprocessor directive do?

16. For the following output statement, the compiler reports this error message: UNDECLARED IDENTIFIER: cout.

cout << “Hello everybody!”  << endl;

The program includes the statement

using namespace std;

What is the most likely source of this error?

17. What is the name of the C++ construct that begins with { and ends with }?

18. How do you write a string that is too long to fit on a single line?

19. Reorder the following lines to make a working program.

images

images

images   Programming Warm-Up Exercises

1. Write an output statement that prints today's date and then goes to the next line.

2. Write a single output statement that outputs the following three lines:

He said, “How is that possible?”
She replied, “Using manipulators.”
“Of course!”  he exclaimed.

3. Write declarations for the following:

a. A named constant, called ANSWER, of type string, with the value “True”

b. A char variable with the name middleInitial

c. A string variable with the name courseTitle

d. A named char constant, called PERCENT, with the value ‘%’

4. Change the three declarations in the PrintName program on page 64 so that it prints your name.

5. Given the following declarations:

const string PART1 = “Pro”;
const string PART2 = “gramming and “;
const string PART3 = “blem Solving with C++”;

Write an output statement that prints the title of this book, using only the cout stream, the stream insertion operator, and the above-named constants.

6. Write an expression that results in a string containing the title of this book, using only the concatenation operator and the declarations of Exercise 5.

7. Write C++ output statements to print the following exactly as shown (a portion of the text found in the preface of this book, relating the action of a weaver's loom to the complexity of human thought). You should write one output statement for each line of text.

Yet the web of thought has no such creases
And is more like a weaver's masterpieces;
One step a thousand threads arise,
Hither and thither shoots each shuttle,
The threads flow on unseen and subtle,
Each blow effects a thousand ties.

8. Fill in the missing lines in the following program, which outputs:

images

images

9. Enter and run the following program. Be sure to type it exactly as it appears here, but substitute your name and the date as indicated.

images

images   Programming Problems

1. Write a program that prints out your course schedule for a single week. Here's an example of the output for one day:

Monday 9:00 Computer Science 101
Monday 11:00 Physics 201
Monday 2:00 Dance 153

   Use named string constants wherever possible to avoid retyping any words or numbers. Be sure to include appropriate comments in your code, choose meaningful identifiers, and use indentation as we do with the programs in this chapter.

2. Write a program that prints out all six permutations of the ordering of the following three lines. Declare a named constant for each line and write an output statement for each permutation. Be sure to include appropriate comments in your code, choose meaningful identifiers, and use indentation as we do with the programs in this chapter.

I saw the big brown bear.
The big brown bear saw me.
Oh! What a frightening experience!

3. Write a program that displays a checkerboard pattern made of stars and blanks, as shown below. A checkerboard is eight squares by eight squares. This will be easier if you first declare two named string constants representing the two different row patterns. Be sure to include appropriate comments in your code, choose meaningful identifiers, and use indentation as we do with the programs in this chapter.

images

4. Write a program that prints out business cards for yourself. A card should include your name, street address, phone number(s), and email address. You also can make up a company name and put that on the card if you wish. To save paper, the program should print eight cards per page, arranged in two columns of four cards. To reduce typing, you should declare a named string constant for each line of the card, and then write output statements to print the eight cards using those constants. Be sure to include appropriate comments in your code, choose meaningful identifiers, and use indentation as we do with the programs in this chapter.

images   Case Study Follow-Up

1. What change would you have to make to the Chessboard program to cause the black squares to be printed with the “%” character instead of “*”?

2. How would you change the Chessboard program to print periods in the white squares instead of blanks?

3. How would you change the Chessboard program if you wanted to reverse the colors (that is, make black squares white, and vice versa) without changing the constant declarations or the values of whiteRow and blackRow?

4. Change the Chessboard program so that the squares are 10 characters wide by 8 rows high.

5. The organizers of the chess tournament find it difficult to write in the black squares of the printed chessboard. They want you to change the program so that the black squares have a blank space in their center that is four characters wide and two lines high. (Hint: You may have to define another string constant.)

6. How many characters are stored in each of the string variables in the Chessboard program?

1. As noted in Chapter 1, most programming languages use the ASCII character code. C++ provides the data type wchar_t (for “wide character”) to accommodate larger character sets such as Unicode. In C++, the notation L‘something’ denotes a value of type wchar_t, where the something depends on the particular wide character set being used. We do not examine wide characters any further in this book.

2. In C++ terminology, it is said that the stream expression is “appended to” the output stream

3. As with our earlier demonstration programs, we could have declared these constants within main. Here we place them before main as an example of what are called global declarations. We return to this topic in a later chapter.

4. In the C language and in pre-standard C++, the standard header files end in the suffix .h (for example, iostream.h),where the h suggests “header file.” In ISO/ANSI C++, the standard header files no longer use the .h suffix.

5. The output lines are shown next to the output statement that ends each of them. There are no blank lines in the actual output from these statements.

6. The invalid concatenation expression “H1” + “there” results in a confusing syntax error message such as INVALID POINTER ADDITION. For now, just keep in mind that such a message may indicate a concatenation expression that doesn't include a variable.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.138.202