Thursday, 1 March 2012

Code your First C++ program

Dear readers,


From last some days I started learning c++ .what I learned that I want to share with you here.I hope in nearest you and I become a C++ programmer.Best of luck.


Before starting let me confirm  you have c++ in your PC or not.If you don't have please go to my previous post. there was I given a link.you can download and install C++ using that link.


After a successful installation of C++. start the program.and type the syntax that given below on your screen.


#include<iostream.h>
void main()
{cout<<"Welcome to C++ Programming";}


Dear readers, it is a simple program program to start in C++. A program to show "Welcome to C++ Programming" on your screen.To see the program's output, after typing the syntax in your screen without doing mistake press Ctrl button+ F9 button. You can see a glance blinking your screen suddenly after that.Press Alt button + F5. Now you can see the message "Welcome to C++ Programming" on your screen.


That is all. You finished your first C++ program.cool! isn't it?


OK now think about what happened after pressing  Ctrl button+ F9 button  and pressing Alt button + F5.


Let me explain,


The first line "#include<iostream.h>" is a specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program. In C++ we have to include such type of directives for the preprocessor according to the program we are going to make.For example for to clear the screen we have to use "#include<conio.h>"
to confirm it in your mind run the previous program one more,you can see the previous output (message"
in your screen.For clearing the previous output from your screen you can include this file on the start up.


#include<iostream.h>
#include<conio.h>
void main()
{clrscr();cout<<"Welcome to C++ Programming";}

include the new lines like i given above syntax and run the program again you can see the difference. I will give more idea to you about these type  of files in C++ latter on our section. 

Now the second line "void main()".



This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function.

The word main is followed in the code by a pair of parentheses (()). That is because it is a function declaration: In C++, what differentiates a function declaration from other types of expressions are these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within them.

Right after these parentheses we can find the body of the main function enclosed in braces ({}). What is contained within these braces is what the function does when it is executed.

Now the last line "cout<<"Welcome to C++ Programming"; ".

This line is a C++ statement. A statement is a simple or compound expression that can actually produce some effect. In fact, this statement performs the only action that generates a visible effect in our first program.

"cout" ( c out) is the name of the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters (in this case the "Welcome to C++ Programming" sequence of characters) into the standard output stream (cout, which usually corresponds to the screen).

cout is declared in the iostream standard file, so that's why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code.

Notice that the statement ends with a semicolon character (;). This character is used to mark the end of the statement and in fact it must be included at the end of all expression statements in all C++ programs (one of the most common syntax errors is indeed to forget to include some semicolon after a statement).

You may have noticed that not all the lines of this program perform actions when the code is executed. There were lines with directives for the compiler's preprocessor (those beginning by #). Then there were lines that began the declaration of a function (in this case, the main function) and, finally lines with statements (like the insertion into cout), which were all included within the block delimited by the braces ({}) of the main function.

The program has been structured in different lines in order to be more readable, but in C++, we do not have strict rules on how to separate instructions in different lines. 

For example, instead of 

#include<iostream.h>
#include<conio.h>
void main()
{
              cout<<"Welcome to C++ Programming";
}

in other format,

#include<iostream.h>
#include<conio.h>
void main() {cout<<"Welcome to C++ Programming";}



I think it gives you a good idea about the C++ structure.


You can also include a " getch()" function in the program. like this,

#include<iostream.h>
#include<conio.h>
void main()
{
              cout<<"Welcome to C++ Programming";
              getch();
}


run the program again and see the difference.For this time you can avoid pressing Alt +F5 to see the program's output.

For this time I am signing off.Thanks for your valuable commends and visit on my page.

Sidh

          

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Web Hosting Coupons