|
||||||
Every C++ program has the same structure and contains three key areas: the preprocessor; comments; and functions. These are essential to any good programmer.
C++ can be a confusing language for a new programmer. However, it is always worth remembering that no matter how complex a C++ program is, it actually just breaks down into three key sections. The sections that make up any C++ program are:
If a programmer includes each of those in their program then they cannot go far wrong. The PreprocessorBefore any C++ program can be run it must first be compiled. This is done using another programmer known as a compiler. The compiler calls a second program known as the preprocessor. The preprocessor examines the code looking for any lines that start with a hash (#). It then uses the information on these lines to modify the code prior to the compilation of the program. Typically the preprocessor is used to load code from other C++ files, for example: #include <iostream.h>
Here the iostream file will include additional functionality in the new C++ program (it will, for example, allow the programmer to write to the screen by accessing the cout method). The reason for doing this is, of course, to enable the programmer to reuse the same functionality in different programs. CommentsComments are an important part of a program. If the code is the 'how' of a program then the comments are the 'why'. But this is an area often neglected by many programmers. It is often ignored for a number of reasons, such as:
However, it is often the case that what's clear to one programmer may not be quite so obvious to another one. Even the same programmer may not understand why they carried out a task in a particular way when they review the code six months or a year later. The comments can be one of two types:
For example: //The C++ comment for single line comments
/*
The C comment
This allows multi-line comments
*/
And finally the programmer is ready to write their own code. FunctionsThe comments define the "why" of a C++ program. Its "how" is built into its functions. However, before writing lots of different functions the programmer must understand that C++ always expects to see at least one function. This compulsory function is called main which must always return an integer (int) value: int main () {
cout << "A simple C++ Program\n";
return 0;
}
The main function is automatically called when a C++ program is run and may be self contained (as shown above) or may call other functions created by the programmer: int displayOutput () {
cout<< "Extend the C++ Program\n";
return 0;
}
int main () {
displayOutput();
return 0;
}
With just those few elements the C++ programmer is read to move on and create the most complicated of programs. SummaryAll C++ programs should contain three elements:
If the programmer includes each of these elements in a C++ program then they will produce a program that is easy to develop and maintain in a single or multi-programmer environment.
The copyright of the article Understanding the Parts of a C++ Program in C Programming is owned by Mark Alexander Bain. Permission to republish Understanding the Parts of a C++ Program in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||