Understanding the Parts of a C++ Program

Examining the Structure of C++ Code

© Mark Alexander Bain

Feb 21, 2009
The 3 Elements of a C++ Program, Mark Alexander Bain
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:

  1. the preprocessor
  2. comments
  3. Functions

If a programmer includes each of those in their program then they cannot go far wrong.

The Preprocessor

Before 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.

Comments

Comments 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:

  • a programmer may feel that they do not have the time to add comments
  • a programmer may feel that that they understand what they are doing and therefore do not feel the need to document that.

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:

  • single line comments (often called C++ comments) - denoted by a double forward slash (//) at the start of the line
  • multiple line comments (often called C comments) - denoted by a forward slash and a star ( /* ) at the start of the comment and a star followed by a forward slash ( */ ) at the end

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.

Functions

The 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.

Summary

All C++ programs should contain three elements:

  1. the preprocessor - this loads any required files and code modifications prior to compilation. It works with lines starting with a hash (#)
  2. comments - the "why" of a program. These are often overlooked but are essential. Comments can be single line (//) or multi-line (/*...*/)
  3. functions - the "how" of a program. There is only one compulsory function called - the main function.

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.


The 3 Elements of a C++ Program, Mark Alexander Bain
A Simple C++ Program, Mark Alexander Bain
Compiling and Running a C++ Program, Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo