Sunday, December 15, 2013

STRUCTURE OF C++ PROGRAM

Parts of C++ program:

  1. DOCUMENTATION SECTION: It includes the comments that improves the readability of the program. It is and non executable statement that helps to read and understand a program. A comment is a message that exists only for the programmer and is ignored by the compiler.                                   TWO TYPES OF COMMENTS:                                                                                                           1. SINGLE LENE COMMENT: It starts with double slash (//) symbol and terminates at the end of        current line. For example: // calculate sum of two numbers.                                                        2. Multiline Comment: C++ programmer can also use C comment style that begin with /* and end with */. For example: /* calculate sum of two numbers.*/                                                         RULES FOR WRITING COMMENTS:                                                                                          a) Multiline comments cannot be nested.                                                                                                /* calculate /* sum of */ two numbers */                                                                                        b) A single line comment cannot be nested within the multiline comment.                                                  /* calculate sum and // product of two numbers. */                                                                         c) Any number of comments can be given at any place in the program but a programmer                         should avoid writing unnecessary comments.                                                                                    d) You cannot use comments within the looping and decision making statements.                                      for(i=0;i<n// i less than n or equal to n;i++)                                                                                     e) Comments should never include special characters such as form feed and backspace.
  2. PREPROCESSOR DIRECTIVE SECTION: It includes various preprocessor directives. All the preprocessor directives starts with a hash sigh (#). A preprocessor directive is a message to the compiler and it is handled by a part of the compiler known as a Proprocessor. The preprocessor directives are processed by the preprocessor before that program is actually compiled. The main function of the preprocessor is to include other files in the source file, specifying symbolic constants etc. Some of the most common proprocessor directives are:                                                               a) #include directive: It tells the preprocessor to include the contents of specified file in the sourcefile      in place of the directive before compilation.                                                                                                #include<iostream.h>                                                                                                                Here, the #include directive tells the preprocessor to include the contents of iostream.h file into          the source file. The iostream.h is a header file that contains the declaration of all the standard              Input/Output functions of      C++. The compiler will generate errors if this file is not included in            the source code.                                                                                                                       b) #define directive: The symbolic constants can be specified using #define. The symbolic constants        allows the programmer to define a name for constant and use that name throughout the program.          For example: #define PI 3.14159. It replaces every occurence of symbolic constant PI with a             value 3.14159 before the program is compiled.
  3. CLASS SECTION: It describes information about user-defined classes present in the program. A class is a collection of data (variable) and functions that operate on that data. A class declaration or defination include variable declaration, function declaration or the defination (code) of the function. The defination of the function may be also defined outside the class declaration i.e. in the class function defination. The function defination provides information to the compiler regarding the name of the function, required arguments and return type of the function. The function defination is that part of the function where the code of the function is defined.
  4. MAIN PROGRAM SECTION: The execution of the program always starts from this part of the program. It includes local declaration  part and executable part. The local declaration part include the declaration of variable that can only be accessed within main(). The executable part consists of statements that are executed in a given order.                                                                                            int main()                                                                                                                                            {                                                                                                                                                                     //local declaration part                                                                                                                       //executable part                                                                                                                     }  
  5. SUBPROGRAM SECTION: It includes various user-defined functions. These user defined functions are nor the part of any class.

No comments:

Post a Comment