4.1 Basic Program Structure

I l @ ve RuBoard

The basic elements of a program are the data declarations, functions, and comments. Let's see how these can be organized into a simple C++ program.

The basic structure of a one-function program is:

 /********************************************************   *   Heading comments   *   ********************************************************/   data declarations   int main(  )  {   executable statement   s      return(0);  } 

The heading comments tell the programmer all about the program. The data declarations describe the data that the program is going to use.

Our single function is named main . The name main is special, because it is the first function called. Any other functions are called directly or indirectly from main . The function main begins with:

 int main(  )  { 

and ends with:

 return(0);  } 

The line return(0); is used to tell the operating system that the program exited normally (status=0). A nonzero status indicates an error ”the bigger the return value, the more severe the error. Typically 1 is used for most simple errors, such as a missing file or bad command-line syntax.

Now let's take a look at the "Hello World" program (Example 2-1).

At the beginning of the program is a comment box enclosed in /* and */. Next we have the line:

 #include <iostream> 

This tells C++ that we want to use the standard input/output system. (The proper name for this is the I/O streams module.) This is a type of data declaration. [1]

[1] Technically, the statement causes a set of data declarations to be taken from an include file. Chapter 10 discusses include files.

The main routine contains the instruction:

 std::cout << "Hello World\n"; 

This instruction is an executable statement telling C++ to write the message "Hello World" on the screen. The special character sequence \n tells C++ to write out a newline character. C++ uses a semicolon to end a statement in much the same way we use a period to end a sentence . Unlike line-oriented languages such as BASIC, the end of a line does not end a statement. The sentences in this book can span several lines ”the end of a line is treated as a space separating words. C++ works the same way. A single statement can span several lines. Similarly, you can put several sentences on the same line, just as you can put several C++ statements on the same line. However, most of the time your program is more readable if each statement starts on a separate line.

We are using the standard object std::cout (console out) to output the message. A standard object is a generally useful C++ object that has already been defined and put in the standard library. A library is a collection of class definitions, functions, and data that have been grouped together for reuse. The standard library contains classes and functions for input, output, sorting, advanced math, and file manipulation. See your C++ reference manual for a complete list of library functions and standard objects.

"Hello World" is one of the simplest C++ programs. It contains no computations , merely sending a single message to the screen. It is a starting point. Once you have mastered this simple program, you have done many things right. The program is not as simple as it looks. But once you get it working, you can move on to create more complex code.

I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

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