Friday, January 29, 2010

Lesson 02 : An example C++ Program

The most efficient way to start learning a programming language is to write programs. C++ program consists with collection of commands (commands can be either functions or keywords) and those are called C++ source code. C++ gives common functions and keywords which can be used throughout the programming.

Let us start our tour of C++ with a simple program. Here is a simple program which outputs a line of text.

// My first C++ program

#include

using namespace std;

int main()
{
cout << "Hello World!" << endl;
return 0;
}

// My first C++ program - This is a comment in C++ language. Comments can also be written starting with a slash followed by a star, and ending with a star followed by a slash. Those don’t have any effect on the program and used to enhance the readability of the program.

#include - This statement call for the inclusion of a header file to the program. A header file consists with pre defined functions and variables. Those can be used more than one program. So this is a preprocessor directive. This is for the declarations of the basic standard input-output library in C++ and tells the preprocessor to include the iostream standard file.

using namespace std; - By using this compiler can use group of functions in the standard library (std). Here you are including a namesscape to your program where all the elements of the standard C++ library are declared. The semicolon is a part of the program and it is a part of the syntax of C++. By using a semicolon you are telling the compiler that you came to the end of a particular command.

int main() – C++ programs are collection of functions and this is the main function in a particular program. Each and every program in C++ has only one main function and many other functions. This is the starting point of an execution of a program, independently of its location within the source code.
The word main is followed by a pair of parentheses and it is the function declaration in C++. Right after the parentheses finds the body of the main function followed by braces. The body of the function consists with what the function does when it is executed. Key word int is used to specify the return type of the function. This returns an integer type.

cout << "Hello World!" << endl; - This is a statement in a C++ program. This gives a visible effect in the program by outputting “Hello World!"”. As Described earlier this should also finished using a semicolon to inform the end of the statement. cout is used to declare the standard output stream in C++. This insert a sequence of characters into the standard output stream.

Operator << is the insertion stream operator. It sends contents of variable on its right to the object on its left. In our case, right operand is the string “My first C++ program” and left operand is cout object. So it sends the string to the cout object and cout object then displays it on the output screen.

endl is the line feed operator in C++. It acts as a stream manipulator whose purpose is to feed the whole line and then point the cursor to the beginning of the next line. We can use \n (\n is an escape sequence) instead of endl for the same purpose.

return 0; - This is the function return statement. This causes the main function to finish. This statement exits the function main indicating its completion and returns the value 0 to the operating system indicating that the program successfully ran to completion. It returns a variable or value to the process that calls this function.

1 comment:

v7soft said...
This comment has been removed by the author.