Structure of a C++ program
// my first program in C++
#include <iostream.h>
int main () { cout << " Hello World "; return 0; }
// this my first program in C++
//: All the lines beginning with two slash signs (//) are considered comments.
#include <iostream.h>
#: Sentences that begin with a pound sign (#) are directives for the
preprocessor. They are not executable code lines but indications for the
compiler.
include: to include.
iostream.h:
standard header file.
This specific file includes the declarations of the basic standard input-output
library in C++, and it is included because its functionality is used later in
the program.
int main ( can include arguments ) { The content of the main function }
Beginning of the main function declaration (begin their execution)
All C++ programs have a main function
In C++ all functions are followed by a pair of parenthesis () that, optionally,
can include arguments.
The content of the main function immediately follows its formal declaration and
it is enclosed between curly brackets ({}).
cout << "Hello World";
cout the standard output stream in C++ (usually the screen).
cout is declared in the iostream.h header file.
(;) signifies the end of the instruction and must be included after every
instruction in any C++ program.
return 0;
The return instruction causes the main ( ) function finish and return the code
that the instruction is followed by, in this case 0.
This it is most usual way to terminate a program that has not found any errors
during its execution.
C++ programs end with a sentence similar to this.
Example:
// This is my first C++ program
/* It's about a hello program
you know what I am saying? */
#include <iostream.h>
int main ()
{
cout << "Hello Aziz!"; // :)
cout << "How are you?";
return 0;
}
aziz. Copyright © by Kuwait Linux User Group - OpenSource free stuff All Rights Reserved. Published on: 2004-08-27 (466 reads) [ Go Back ] |