Structure of Simple C++ Programs


 

Structure of Simple C++ Programs

A C++ program with several functions

/******************************************************
  A program with some functions and comments
******************************************************/
   
#include <iostream>
using namespace std;
   
void line(), message();             // Prototypes
   
int main()
{
   cout << "Hello! The program starts in main()."
        << endl;
   line();
   message();
   line();
   cout << "At the end of main()." << endl;
   
   return 0;
}
   
void line()                       // To draw a line.
{
   cout << "--------------------------------" << endl;
}
   
void message()               // To display a message.
{
   cout << "In function message()." << endl;
} 

Screen output

Hello! The program starts in main().
-----------------------------------
In function message().
-----------------------------------
At the end of main(). 
The example on the opposite page shows the structure of a C++ program containing multiple functions. In C++, functions do not need to be defined in any fixed order. For example, you could define the function message() first, followed by the function line(), and finally the main() function.
However, it is more common to start with the main() function as this function controls the program flow. In other words, main() calls functions that have yet to be defined. This is made possible by supplying the compiler with a function prototype that includes all the information the compiler needs.
This example also introduces comments. Strings enclosed in /* . . . */ or starting with // are interpreted as comments.
Examples:
/* I can cover
   several lines */
// I can cover just one line 
In single-line comments the compiler ignores any characters following the // signs up to the end of the line. Comments that cover several lines are useful when troubleshooting, as you can use them to mask complete sections of your program. Both comment types can be used to comment out the other type.
As to the layout of source files, the compiler parses each source file sequentially, breaking the contents down into tokens, such as function names and operators. Tokens can be separated by any number of whitespace characters, that is, by spaces, tabs, or new line characters. The order of the source code is important but it is not important to adhere to a specific layout, such as organizing your code in rows and columns. For example
void message
      (   ){ cout  <<
         "In function message()."  <<
   endl;} 
might be difficult to read, but it is a correct definition of the function message().
Preprocessor directives are one exception to the layout rule since they always occupy a single line. The number sign, #, at the beginning of a line can be preceded only by a space or a tab character.
To improve the legibility of your C++ programs you should adopt a consistent style, using indentation and blank lines to reflect the structure of your program. In addition, make generous use of comments.

Post a Comment

0 Comments