If you like my posts please leave a comment.

Monday, July 23, 2012

Basic Structure of a C++ program


A basic structure of a C++ program can be better understood with the help of a sample program code. Let’s see the following example and corresponding illustration.
//First program in C++
#include<iostream.h>
int main( )
{
cout<<”Welcome to the world of C++”;
return 0;
}

Comments:

In the above program at first we have this line - // First program in C++
In C++ it is a comment line. All line beginning with two consecutive front slashes (//) are considered as comment lines and is ignored by the compiler during the compilation of the program. C++ also supports old C style comment writing i.e. using of /*…..*/ this is actually called as BLOCK COMMENT or MULTIPLE LINE COMMENT.
Thus in C++ we have two styles of writing comments i.e. Single line comment by using (//) or Multiple line comment or Block comment using (/* … */).

Preprocessor directive:

The line beginning with a hash sign (#) is called preprocessor directive. Before compilation of the program starts it tells the compiler to include the specified header file(s) (iosrteam.h) with the associated source code.
#include tells the compiler to include the source file into your program.

Header files / Standard library header files:

Header files contain prototype, variable, constants declarations and function definition for the associated library functions. In the above sample program “iostream.h” is a header file. It contains necessary definition for basic standard input/output operations.
            The header file iostream.h should be included at the beginning of all programs that uses standard input/output statements (cin / cout). In the context of naming conventions of these header files you may notice some variations. Some implementation may use iostream.h or iostream.hpp some can use iostream.hxx. We should appropriate header files depending on the contents of the program and implementation.
            Here is a list of very commonly used C++ standard library header files.
Header file
Purpose
<assert.h>
Contains macros and information for adding diagnostics that aid the program debugging.
<ctyp.h>
Contains function prototypes for functions that test characters for certain properties and function prototypes for functions that can convert lowercase to uppercase letters and vice versa.
Example: toupper(), tolower, isalpha(), isalnum(), isdigit()
<float.h>
Contains the floating – point size limits of the system.
<limits.h>
Contains the integral size limits of the system.
<math.h>
Contains function prototypes for mathematical functions.
<stdio.h>
Contains function prototypes for standard input/output library functions. Example – gets(), puts()
<stdlib.h>
Contains function prototypes for conversion of numbers to text, text to numbers, memory allocation, random numbers and various other functions.
<string.h>
Contains function prototypes for string processing function.
<time.h>
Contains function prototypes and types for manipulation of time and date.
<iostream.h>
Contains function, prototypes for the standard input and standard output functions
<iomanip.h>
Contains function, prototypes for the data stream manipulation that enable formatting of streams of data.
<fstream.h>
Contains function, prototypes for function that perform input from disk file and output to disk file.


The main( ) function:

In C++ the main( ) function is truly a main function by nature. A C/C++ program must have a main( ) function. This is the function which executes first and all other library or user-defined functions are called from main( ) function. This is the point from where all C / C++ program start their execution. When the main( ) function terminates the program terminates. The body of the main( ) is enclosed in pair of curly braces { }. The braces contain the C / C++ statements that are compiled and executed by C / C++ compiler.  In C++ the function main( ) can have following forms:
  • <return-type> main( ) //Form 1
Example: int main( )
  • main( ) //Form 2
In Form 1 the type of data to be returned by function main( ) is mentioned (int)
In Form 2 the type of data to be returned by function is not explicitly declared. However in C++ by default every function returns a data of type int by default.
            EMore on C++ data types and how they are associated with function return types will be discussed in the subsequent chapters.

cout

In C++ identifier cout represents the standard output stream. It inserts a sequence of characters into the standard output stream (usually screen). It is declared in iostream.h standard header file.
            Action of out is reversed by cin; represents the standard input stream. It reads data from keyboard and assigns to variable.

Use of Semicolon(;):

In C / C++ a semicolon (;) marks the end of the statement. It must be included at the end of each C/ C++ statement.

‘return' – statement:

The return statement causes the main (practically any C/C++ function) function end. Usually a return
statement is followed by return code. Here in this program it is 0 (Zero). A return code 0 means that the program worked as expected without any error during the execution.  This is the most usual way to end a C++ program. However if return type of main( ) function is declared as void. It is not necessary to write a return statement with a return code or simple return can be written without a return code.
Example 1:
//First program in C++
#include<iostream.h>
void main( )
{
cout<<”Welcome to the world of C++”;
return; //return statement without any return code
}
Example 2:
//First program in C++
#include<iostream.h>
void main( )
{
cout<<”Welcome to the world of C++”;
// here no return statement is present
}