Chapter: Introduction to writing programs (C++)


Some Basics of programming
  1. Always know completely the working of the methods and statements you use. Most, of the time new learners do not know the complete behaviour of the methods or statements they use.
  2. Divide your program into 3 basic sections - declaration section, initialisation section and the body.
    Declaration Section: This Section should be the first section of your program. It contians all the variable (or identifier) declarations.
    Initialisation Section: This Section should be the second section of your program. It contains all the variable (or identifier) initialisation statements. It is a good practice to initialise your variables (or identifiers). If any variable (or identifier) is not initalised, then it may assume any random value also referred  to as garbage-value. This might result into unexpected run-time errors.
    Body: This is the last and the main section of your program. It contains the actual statements and instructions to the compiler as per the program's logic.

Writing C++ Program


A Sample Program

 #include<iostream> //old version: #include<iostream.h>

using namespace std; //old version: omit this line/statement

int main()
{
      cout<<"Hello!!! Welcome to C++";
      return 0;
}

Note: 
New C++ version (ANSI C++) and compilers used the following;
1. header files declared as 
     #include <headerfilename.h>
     Exception: "iostream.h" is  included as "iostream" in modern ANSI C/C++ compilers
2. must declare a namespace object as follows
          using namespace std;
3. main method/function declared as follows;
          int main()
          {
               //statements
               return 0;
          }

Old C++ version and/or compilers used the following;

1. header files declared as 
     #include <headerfilename.h>
2. main method/function declared as follows;
          void main()
          {
               //statements
          }