Monday, August 16, 2010

Programming With C++ Language

Hmmm... what should I say yeh??? Actually, these explanation I took/copied from http://www.cprogramming.com/tutorial/lesson1.html.

From this site, " a C++ program is a collection of commands, which tell the computer to do "something". This collection of commands is usually called C++ source code, source code or just code. Commands are either "functions" or "keywords". Keywords are a basic building block of the language, while functions are, in fact, usually written in terms of simpler functions.

Think of it a bit like an outline for a book; the outline might show every chapter in the book; each chapter might have its own outline, composed of sections. Each section might have its own outline, or it might have all of the details written up.) Thankfully, C++ provides a great many common functions and keywords that you can use.

But how does a program actually start? Every program in C++ has one function, always named main, that is always called when your program first executes. From main, you can also call other functions whether they are written by us or, as mentioned earlier, provided by the compiler.

So how do you get access to those prewritten functions? To access those standard functions that comes with the compiler, you include a header with the #include directive. What this does is effectively take everything in the header and paste it into your program. "

Hahahahaha..sorry for just putting down some copied resources... By the way, just look at the example below. I have noted a simple explanation at the left side of the program.

Eg1. The Example Program of HelloWorld.cpp

The explanation. Please refer to the program code that is written in a yellow box. I love to refer this program source codes that's using Microsoft Visual C++ Compiler. In this compiler, we can see the differences by looking at difference colour of statement.

Comment. Usually we can recognize comment in green. Actually there are two types of comments in Microsoft Visual C++. Comment for a single line or comment for multilines.
If we want to write comment for a single line, we just start with  // at the beginning of the comment line.For examples
            
            //include this file for cout
            //Filename: HelloWorld.cpp

Got it? ok now, let's look at a comment for multiple lines. If there is so, we use the /* symbol start the comment and we end the comment with */. Looks easy?. Look at the example

           /*My first program in this
             basic programming course is start with Hello World!*/

Have you seen the difference??

Ok next syntax is a preprocessor directive. We always used #include to write a command for preprocessor directive (in blue colour). This command allowed us to embed some library files or header files into our own program or else, we actually call for another program files that recognized as a header files into our own file.  As an example

          #include<iostream.h>
          #include<math.h>

Next is a main function. For every C++ program there will be only one  main function. main is a name for a function and from a main function, we can call another function that written by us. 

Then, we look at the body of a function. We will start the block function with { symbol and at the end of the block we should end with } symbol. (I call these { and } symbol as a curly braces). So, our program coding will be written between these two braces.

So, this is what C++ all about... It just a beginning. There will be more syntax afterwards in next entries.

Steps in Programming

There are 6 steps in programming

  1. Defining and analyzing problems
    • Programming begin with a specification of problems.
    • This steps is to identify and understand what are the problems to resolve.
    • The problems must be clearly define, explicit and the requirements in resolving it.
    • Analyzing the problems will determine the input, output and information required to solve these problems, as follows:
      • input – data to be processed
      • output – the desired result
      • the constraint and additional features in resolving the problems
  2. Planning of variables
    • Variables are simply references to memory locations.
    • A well plan use of variables will produce an efficient execution of program in terms of speed and memory utilization.
  3. Drawing the flowchart
    • Flowchart represents algorithm in graphic form comprising of geometrical symbols which is interrelated by line of flow.
  4. Program writing
    • In the design of program it should be written as simple as possible.
    • The main objective is to give a clear, readable programs through an orderly and disciplined approach to programming.
  5. Testing and debugging program
    • Once the program has been written it must be compiled and executed.
    • This is accomplished by an editor and compiler.
    • An editor lets us type a program, makes changes and save it to a file.
    • The compiler then translates the program into a form that the computer can read.
    • Once the program has been compiled and executed the presence of errors will be readily apparent.
    •  There two types of errors
      • Syntactic and execution errors
        • usually result in the generation of error when compiling or executing a program.
        • Error of this type is usually quite easy to find and correct.
      • Logical errors
        • Since the output resulting from logically incorrect program may appear to be error free.
        • Thus a good bit of probing may be required which is known as logical debugging
  6. Documentation of program
    • Program must be documented for future references and maintenance process.
    • A well documented program will make it easier for the original programmer or other programmer to follow the program logic and design.
    • Program document should consist of :
      • An accurate specification of requirement
      • Detail input, output, constraint and formula for the above problems
      • Algorithm in the form of flowchart or pseudo code
      • Program source complete with comment
      • Sample program which had been run and executed and the tested data.
      • Guideline on how to use the program.