Let’s take a look at a very simple C++ program and find out what its constituents are. You don’t need to enter this code right now; it’s just here so that you can get a feel for what goes into making up a program. I don’t go into all the details at the moment either, as everything that appears here will be explored at length in later chapters. Figure 1-2 illustrates a simple C++ program.
The program shown in Figure 1-2 displays the following message: ============================================================ This isn’t a very useful program, but it serves to demonstrate a few points. The program consists of a single function, main(). A function is a self-contained block of code that’s referenced by a name, main in this case. There may be a lot of other code in a program, but every C++ application consists of at least the function main(). There can be only one function called main() within a program, and execution of a C++ program always starts with the first statement in main(). The first line of the function is
which identifies that this is the start of a function with the name main. The int at the beginning indicates that this function will return an integer value when it finishes executing. Because it’s the function main(), the value will be received by the operating system that calls it in the first place. This function main() contains two executable statements, each on a separate line:
These two statements are executed in sequence. In general, the statements in a function are always executed sequentially, unless there’s a statement that specifically alters the sequence of execution. You’ll see what sorts of statements can do that in Chapter 4. In C++, input and output are preferably performed using streams. If you want to output something from a program, you put it into an output stream, and when you want something to be input, you get it from an input stream. A stream is thus an abstract representation of a source of data, or a data sink. When your program executes, each stream is tied to a specific device that is the source of data in the case of an input stream and the destination for data in the case of an output stream. The advantage of having an abstract representation of a source or sink for data is that the programming is the same regardless of what the stream actually represents. You can read data from a disk file in essentially the same way as you read from the keyboard, for instance. The standard output and input streams in C++ are called cout and cin, and by default they correspond to your computer’s screen and keyboard, respectively. The first line of code in main() outputs the character string “The best place to start is at the beginning” to your screen by placing it in the output stream, cout, using the insertion operator,<<. When we come to write programs that involve input, you’ll see its partner, the extraction operator, >>. A header contains code defining a set of standard facilities that you can include in a program source file when required. The facilities provided by the C++ standard library are stored in headers, but headers aren’t exclusively for that. You’ll create your own header files containing your own code. The name cout referred to in this program is defined in the header iostream. This is a standard header that provides the definitions necessary for you to use the standard input and output facilities in C++. If your program didn’t include the following line:
then it wouldn’t compile, because the <iostream> TIP Note that there are no spaces between the angled brackets and the standard header name.With many compilers, spaces are significant between the two angled brackets, < and >; if you insert any spaces here, the program may not compile. The second and final statement in the body of the function name is return 0; This ends the program and returns control to your operating system. It also returns the value zero to the operating system. Other values can be returned to indicate different end conditions for the program and can be used by the operating system to determine if the program executed successfully. Typically, zero indicates a normal end to a program, and any nonzero value indicates an abnormal end. However, whether or not a nonzero return value can be acted upon will depend on the operating system concerned. NamesLots of things in a C++ program have names that are used to refer to them. Such names are also referred to as identifiers. There are five kinds of things that you’ll give names to in your C++ programs:
In C++, you can construct a name using the upper- and lowercase Latin letters a to z and A to Z, the underscore character (_), and the digits 0 to 9. The ANSI standard for C++ also permits Universal Character Set (UCS) characters to be included in a name for reasons I cover in a moment. The ANSI standard allows names to be of any length, but typically a particular compiler will impose some sort of length limit. However, this is normally sufficiently large (several thousand characters) that it doesn’t represent a serious constraint. Whitespace is the term used in C++ to refer to spaces, vertical and horizontal tabs, and newline and form-feed characters. You must not put whitespace characters in the middle of a name. If you do, the single name won’t be seen by the compiler as such; it will be seen as two or more names, and therefore it won’t be processed correctly. Another restriction is that names may not begin with a digit. Here are some examples of legal names:
Here are some names that aren’t legal:
CAUTION Note that names that contain a double underscore (_ _) or start with an underscore followed by an uppercase letter are reserved for use by the C++ standard library, so you shouldn’t choose such names for use in your programs.Your compiler probably won’t check for this, so you’ll only find out that you have a conflicting name when things go wrong!
blog comments powered by Disqus |
|
|
|
|
|
|
|