5.1 The First Program


5.1 The First Program

image from book
Figure 5.1

To begin, let's examine a sample C++ program-one of the simplest that can be written. This sample program is known as the "Hello World" program because, when executed, the program will print those words onto the screen. The C++ code follows, and subsections explain the basics of each line. These will be explored in more detail later in this book.

      //My First C++ Program      #include <iostream>      int main()      {        std::cout << "Hello world!\n";        return 0;      } 
Note 

As with many Western languages, C++ statements are generally read by humans from left to right. The flow of execution (the order in which a computer will run the program) is from top to bottom, unless the program specifies that it should jump to somewhere else (as we shall see later).

Compile this program in your IDE and run it.

Note 

Some IDE environments, like Visual Studio .NET, by default will close the application and make the window disappear as soon as the program is finished, which means the results cannot be seen easily. To prevent this, the following two lines can be added to a program.

      char key = 0;      std::cin >> key; 

5.1.1 Comments

      //My First C++ Program 

Many C++ programs are littered with comments, and our sample program is no exception. The first line of our program is a comment, though, like most C++ statements, they may appear on any line, in any order. Comments are prefixed with the characters "//" and the rest of the line contains whatever comments a programmer wishes to write. Comments are ignored by the compiler and serve no practical function in a program. They are included to aid the programmer in the same way sticky notes are used as reminders.

Comments can span multiple lines, but each line must begin with "//" to indicate it is a comment. If a programmer anticipates a comment could take a great many lines, it can become tedious to continually prefix lines in this way. In such cases, the "//" character can be omitted, and instead "/*" can mark the beginning of a comment and "*/" can mark the end of a comment. Consider the following code:

      /*This      is all a      comment*/ 

5.1.2 Preprocessor Directives

      #include <iostream> 

The compiler assembles the C++ code and ensures it's translated into a form the computer understands. Preprocessor directives are C++ commands that begin with a "#" character, and they typically appear at the top of code files. They are instructions that define various rules and conditions that the compiler must consider before assembling the majority of the code. The above preprocessor directive is called #include. Its purpose is to include C++ code from other files. It conceptually takes all the code from one file and copies it into the current program file, as though it all were part of the same program. In this case, the code is from a file called iostream.

5.1.3 Functions

Here's the sample program again:

      int main()      {         std::cout << "Hello world!\n";         return 0;      } 

One of the most fundamental features of C++ is functions, and a more formal definition of them will be given later. For now, it is convenient to think of them as a container for other code, much like a drawer contains files. Each function has a name (in this case main) and each function also has beginning and ending braces ({}) that are used to mark the beginning and end of contained code. The code within these braces is contained within the function. A program may have many functions that contain all kinds of code. But a C++ program must have at least one special function, and this function is main. This is where the program begins. Execution begins at the top and, line by line, works its way toward the bottom where execution will end.

Note 

Most lines inside a function are called statements. Statements always end with the semicolon character (;).

      std::cout << "Hello world!\n";      return 0; 

5.1.4 Printing Text

Those who experiment with the std::cout statement by substituting text other than "Hello world" will find that whatever text is substituted is the text printed on the screen when the application is executed. std::cout is a command that outputs text to the screen. The text that follows the << symbol is the text to be printed. In C++ the text contained in quotations is called a string, for reasons explained later, and the final "\n" character at the end of the string is a line terminator. It indicates the end of a single line. If a string such as "Hello world!\n and mum too" were substituted, the text "and mum too" would appear on a new line.

Note 

The statement "return 0;" is related to a function. As explained later, a function can accept input values and can also output values. The return statement is a means of outputting a value. In this case, since we have nothing valuable to give out, we simply "return" 0.




Introduction to Game Programming with C++
Introduction to Game Programming with C++ (Wordware Game Developers Library)
ISBN: 1598220322
EAN: 2147483647
Year: 2007
Pages: 225
Authors: Alan Thorn

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net