Structure of a program

1
Want create site? Find Free WordPress Themes and plugins.

Learning a programming language is quite easy from many ways but the simplest way is by writing programs. At the intital or first program for beginners is called “Hello World” which will be simply printing “Hello World” on your computer screen. Although this is a simple program, it contains all fundamental components that the C++ program is having.

Structure of a program

1      // my first program in C++                                                              Hello World

2

3      #include <iostream>

4

5      int main()

6      std::cout << “Hello World!”;

7      {

}

 

The left panel that is above shows that the C++ code to this program, the right panel show the result when the program is executed. The numbers that are the panels are line numbers to make discussing programs and also researching errors easier which are not part of the program.

Line 1: // my first program in C++

The two slash signs show the remaining of lines that is comment and inserted by coder or programmers. It doesn’t have any effect on program. The programmer uses them to include brief explanations or evaluations that are concerning the code or program.

Line 2:  #include <iostream>

A sign (#) is using at the beginning of lines that is directives read and can be translated by pre-processor. Some of the special lines that are interpreted before execution of the program. In this case the directive #include <iostream>, that instructs the preprocessor for including a section of standard C++ code that is called header iostream. Afterall it performs standard input and output operations like writing the output of this program on the screen (Hello World).

Line 3: A blank line

In the blank line only the readability of the program is done and there is no effect on the program.

Line 4: main ()

This line is important which starts the declaration of a function that is a collective of code statements and given name. It gives the name “main” to the group of code statement and function is introduced with a succession of a type (int), a name (main) and a pair of parenthesis (()) along with a parameter which is optional.

Function name “main” is a special function in all C++ program and it is also running a program. This is the first stage of executing and the execution of all the program is begin with the main function.

Line 5 & 7: {and}

In this line (5) the open brace ({) expresses the starting of main’s function definition and the closing brace (}) in line 7 shows the ending stage. Between these braces everything included is called functions body.

Line 6:  std::cout << “Hello World!”;

In this line it has been stated the C++ statement line and show which can produce some effects on the program. All the Statements are compiling a fix order that they appear within a function’s body. Statement is having three parts that are following

1: std:: cout. This is the part that point out the standard character output device.

2: Insertion operator (<<) This part shows what follows is inserted into std:: cout.

3: Sentence in quotes (“Hello world!”) The last part shows that the content inserts into the standard output.

Note:

Statement ends with a semicolon (;) which marks the end of the statement. It is used just like a period in English sentences.

In the programs all the lines are performing actions when the code is executed, there is a line in comment that is beginning with //. Another line with a directive for the preprocessor that is beginning with # and finally the last line that defines a function called main function.

The structure in a program has a very important role and structured lines are very important because all program has been structured in different lines. It has made very easy to understand by the humans reading. C++ is not having strict rules to get the instructions in different lines. Following are few examples for your understanding,

1
2
3
4
int main ()

{std::cout << ” Hello World!”;

}

 

It is the same we could have written like, int main () {std::cout << “Hello World!”; }. So all are in a single line and having the same meaning as the preceding code.

In C++ separation between statements known as with ending semicolon (;) some of the statements can be written in a single line, or it’s also possible to write each statement in its own line. The division of code in different lines serves just to make it easy for reading.

This is a statement of our first program,

 

1
2
3
4
5
6
7
8

 

// my second program in C++

#include <iostream>

 

int main ()

{

std::cout << “Hello World! “;

std::cout << “I’m a C++ program”;

}

 

Hello World! I’m a C++ program

 

Here in this program performed two insertions into std::cout in various statements, the separation in different lines of code provide greater readability to the to the statements. Anyhow the main is perfectly is valid defined in this way.

Just have a look to the simple example,

int main () { std::cout << ” hello world! “; std::cout << ” I’m a C++ program “; }

The source code divides into more code lines in place and it will give the same result like in previous.

 

1
2
3
4
5
6
7

 

int main ()

{

std::cout <<

“Hello World!”;

std::cout

<< “I’m a C++ program”;

}

 

Comment

We already have mentioned that comment can’t affect the operation of the program but can gives essential tool to document with source code. The source code is done by the program and know it operator.
C++ is able to support two ways of commenting code:

1

2

// line comment

/* block comment */

 

First one is known as Line Comment and discharged everything from where the pair of slash signs (//), the second one is known as Block Comment that discards everything between the /* characters and first appearance of the */ characters.

Now here lets add comment to second program:

 

1
2
3
4
5
6
7
8
9
10

 

/* my second program in C++

have too comments */

#include <iostream>

int main ()

{

std::cout << “Hello World! “;  // prints Hello World!

std::cout << “I’m a C++ program”; // prints I’m a C++ program

}

 

Hello World! I’m a C++ program

 

Whenever you includes the comments within source code of a program without using the comment characers combination //, / or /. Then compiler takes them as if they were C++ expressions, this is the reason while compilation creating errors or to fail.

Using of namespace STD

Have you ever seen C++ code? You might have seen cout instead ofstd::cout both are the same object, the first one is using for an unqualified name (cout) and the second is for qualifies it directly within the namespace STD (as std::cout).

In case you want to refer the element in the STD namespace, a program will qualify and the use of elements of the library, or introduce visibility of its components. In addition, the use of declaration in programming is typical way to introduce visibility of these components.

using namespace std;

 

The upper declaration permits the entire elements in the STD namespace to access in an unqualified manner. The last example can write for making unqualified use of cout like;

1
2
3
4
5
6
7
8
9
// my second program in C++

#include <iostream>

use the namespace std;

int main ()

 

{

cout << “Hello World! “;

cout << “I’m a C++ program”;

}

Hello World! I’m a C++ program

 

Conclusion

In C++ the two ways are using to access the elements of the STD namespace are valid. Well, if you keep on learning from our notes so we guarantee that neither collisions nor mistakes will happen. We hope that you will learn enough about C++ from this our notes and article. Finally, you can visit our website on WWW.INTERNETSEEKHO.COM

Did you find apk for android? You can find new Free Android Games and apps.
Share.

About Author

1 Comment

  1. Pingback: 10 best programming languages of 2017 - Internetseekho

Leave A Reply