C++ Language Code Line Concepts

Posted by Bhavesh Joshi On Tuesday, October 25, 2011 0 comments
                                                   
C++

It is depends up on your computer and on your compiler about the process of compiling your program varies. For now, we will suppose that you are using a WINDOWS machine and in windows, you are using the turbo C compiler.

So here is the program code to see string “Hello World” on your output screen:


General Way to Write Program in C++
To run your program, simply press CTRL + F9 key. You would see the following output:
                                                             Hello World

To debugging your program line-wise then, press F7.

After this we can save our program as hello.cpp where .cpp shows c++ program file.

Let’s take a look at each line of code that makes up hello.cpp.

#include iostream.h 

The effect of this line is to essentially "copy and paste" the entire file iostream.h into your own file at this line. So you can think of this syntax as replacing the line #include with the contents of the file iostream.h. #include is known as a preprocessor directive, which will be covered much later. This file is located somewhere in your include path. The include path indicates the directories on your computer in which to search for a file, if the file is not located in the current directory. In this case, iostream.h is a file containing code for input/output operations. You need to include iostream.h so that the compiler knows about the word cout cin, which appears a couple of lines below.

int main() 

Every C++ program must have what is known as a main function. When you run the program, the program will go through every line of code in the main function and execute it. If your main is empty, then your program will do nothing. There are essentially four parts to a function definition. They are the return type, the function name, the parameter list, and the function body, in that order. In this case:

This is the line that prints out the text string, "Hello, World!". For now, don't worry about how cout works; just know how to use it. You can print out any series of text strings. So, write cout<<"Hello, World!";

Cin works as to scan those text strings. The cout and cin functions are system defined functions used to print the words and scan those words means cin used as input and cout as output.

return 0; 

This line is necessary because the return type of main is int (see above). We'll talk more about functions and return types later, but for now understand that because the function's return type is int, the function must return an int (integer). To return 0 (which is an integer, we simply write return 0;.

getch();

getch(); is used to hold output screen as if we compiled our program then, black screen will appear and then, we will easily see our result. Without this, we will press Alt+f5 to visit our output screen. So this is better that once we entered once in our code getch(); and with this, it is compulsory to add #include in our program which shown above in our program.

Finally, these above concepts are the c++ program code lines concepts and all are described well...
READ MORE