Your First C++ Program

 

Listing 2-1

You can consider Listing 2-1 as a viable candidate to be your first C++ program. Let’s look at it in detail.

 

We begin with the preprocessor directive that causes the file iostream to be loaded or pre-pended to the listing that you see. The result of including iostream in this way is just as if you had a single file that was comprised of the iostream header file and the Listing 2-1 file concatenated together into a single file (with the include statement omitted).

 

We will come back to iostream, but first look at the structure of the  program as it is shown on page 23. You see a single function, main, a comment, and an executable statement using cout. The program begins execution with the cout statement and then terminates. If you compile and run this program in the Quincy 99 environment it will display the text between the quotes on the screen. If you compile and run it in Visual C++, it will also compile and run in like fashion, but you will get a warning to the effect that the main function should return a value. That is because the main function header declares that it will return an integer, but there is no code to do that. The Mingw32 compiler, which is used by the Quincy99 environment does not care about that, but the Visual C++ compiler does. So, if you will be working primarily with the Visual C++ compiler, you may want to get into the habit of putting in the statement

return 0;

or

return (0);

 

just to avoid seeing the warning message.

 

There is another difference in the behavior of the two compilers that you will run into from time to time, and that is the global specifier std::. In most cases you could omit that with the Mingw32 compiler but not with the Visual C++ compiler. You will find little differences like that between the compilers, because of the way the namespaces are defined in the two class libraries and because of the way the class libraries are organized. Typically, you will have to either add the std:: global specifier or take if off in order to get the program to compile.

 

As you examine the listing 2-1 in detail, pay particular attention to the curly braces, the semicolons and the syntax of the statement that sends the literal string constant out to the display screen. You will be using statements similar to that extensively before we learn exactly how the stream objectss and the << and >> operators work.

 

Flow

Even though this is a short program with only one executable statement, this is a good time to get used to the idea that everything flows from top to bottom in a program listing. In other words, the control flow is always in the direction top to bottom (unless there is logic that modifies the control flow) and the order In which the compiler processes the listing at compile time is also from top to bottom. So, when we say that a variable must be declared and initialized before it is used, we mean that the declaration must be first as you progress top to bottom in the listing. Its initialization must also be accomplished before it is used in a context in which it is expected to have a value.

 

The <iostream> header

The iostream header file is a file that contains several class declarations and member method definitions, including definitions of classes and methods that define how the cout statement and the << operator cause control to pass to appropriate library routines that do the low level I/O necessary for displaying the output arguments. It is beyond the scope of this course to read the code in the header files or to study the code in library files, but in Chapter 26 we will learn more about I/O Streams.