Pages

Friday, February 4, 2011

Program Structure of C++

A program structure refers to how the program code will be written. The structure is there to set rules that must be followed or otherwise the program will not run or executed due to errors or bugs.


Old style programming
#include<iostream.h>
int main( )
{
//this is a comment (a single line comment)
//your program starts here (the real coding of program for the C++ language)
cout<< "Hello World Wide Web!";
return 0;
}

New style programming
#include<iostream>
using namespace std;
int main( )
{
//this is a comment (a single line comment)
//your program starts here (the real coding of program for the C language)
cout<< "Hello Wordl Wide Web!";
return 0;
}

Old and new style notes:
Some old IDE (integrated Development Environment) uses the old style programming technique for c++. due to the incapability of reading the new styling of programming in c++. Some are due to the standard implementation. Though there are some IDE that adopts the old and new. I leave it up for you to decide which one do you prefer. if you notice that the old uses the .h while the new does not. and the new uses the using namespace std;

This is due to the standard use of syntax in c++ programming.
standard output;
std::cout<<"data here!";

Some IDE also requires the system ("pause") command so that you can see your output before the program closes.

Here are some explanation about the program structure and the code inside it.

1.) The first line refers to preprocessor. It means before the program is executed, it requires some information so the computer can understands what's it gonna do with the program. To be exact, the computer needs to understand first your program codes before it will execute it. Some refers to it as a library or a header.

2.) The second line is called the main function. A function is a part of your program (This will be discuss in another topic). It is the first function that will be read and executed when your program starts its execution.
Notice it has the open curly brace ( { ) and the closing curly brace ( } ). It signifies the beginning of the content and its end of the function.

3.)The cout<<"Hello World Wide Web!";
cout - console output.
<< -  insertion operator. use to insert data.

This line of code will just tell your computer to print something in the screen. for this cout, it will print the "Hello World Wide Web!. What ever the content of this cout will be displayed in your screen. Just DON'T forget about the double quotation mark ( " " ) which enclose your data " Hello World Wide Web! ".
Notice the semicolon ( ; )at the end of the cout. it signifies the end of the line of code (this refers to the line of code of statement!).

4.) return 0
another statement that tells the computer that the program executes without an error!

No comments:

Post a Comment