If you want to learn a programming language so there are many ways to learn it, the simplest way is by writing programs. The first program for beginners is called “Hello World” that will be simply printing “Hello World” on your computer screen. Actually this is a simple program but containing all fundamental components that the C++ program is having -Structure of a program

1
2
3
4
5
6
7
// my first program in C++

#include <iostream>

int main()

{

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

}

Hello World!

 

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

We are going to show you examine of a program line by line.

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

The two slash signs show the remaining of the line which is a comment and inserted by programmer and it doesn’t has any effect on the program. The programmer uses them to include brief explanations or evaluations that are concerning the code or program.

Line 2:  #include <iostream>

Here the lines begin with a sign (#) that is directives read and translated by preprocessor. They are some special lines that are interpreted before compilation of the program. In this case the directive #include <iostream>, instructs the preprocessor to include a section of standard C++ code that is known as header iostream, it allows performing standard input and output operations same like writing the output of this program  (Hello World) on the screen.

Line 3: A blank line

Blank Line only improves the readability of the program and there is no effect on the program.

Line 4: main ()

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

Function name main is a special function in all C++ program and it also running a program, the execution of all the program is begin with the main function.

Line 5 & 7: {and}

In line 5 the open brace ({) shows the starting of main’s function definition and the closing brace (}) in line 7 indicates its end. Between these braces everything included is functions body.

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

This is a C++ statement line and expresses that can produce some effect on the program. Statements are executing in the same order that they appear within a function’s body. Statement has three parts, first std:: cout that point out the standard character output device. Second, the insertion operator (<<) which shows what follows is inserted into std:: cout. The third is a sentence within quotes (“Hello world!”),that is the content inserted into the standard output.

Note:

Statement ends with a semicolon (;) which marks the end of the statement same like a period in an English sentence.

All the lines of the program perform actions when the code is executed, there is a line in comment that is beginning with //. There is another line with a directive for the preprocessor that is beginning with #. There is another line that defines a function which is main function.

Program has been structured in different lines and has made easy to understand by the humans reading. C++ does’t has strict rules on getting to how to get instructions in different lines. Take a look to the example,

1
2
3
4
int main ()

{

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

}

 

The same we could have written like this, int main () {std::cout << “Hello World!”; }

So all are in a single line and the meaning is the same as the preceding code.

In C++ separation between statements known as with ending semicolon (;) and many statements can be written in a single line, or each statement can be in its own line. The division of code in different lines serves only to make it more legible for the humans that can read it.

Here 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 the program performed two insertions into std::cout in different two statements, the separation in different lines of code simply gives greater readability to the program, but main is perfectly valid defined in this way.

Here is the simple example,

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

The source code can have also been divided into more code lines in place:

1
2
3
4
5
6
7
int main ()

{

std::cout <<

“Hello World!”;

std::cout

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

}

 

The result will be the same as the previous one.

Comment

As we mentioned above comment can not affect the operation of the program but can provide important tool to document within the source code that the program does and know it operators.
C++ supports two ways of commenting code:

1
2
// line comment

/* block comment */

 

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

Now lets add comment to second program:

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

with more 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

When comments are included within the source code of a program without using the comment characters combinations //, / or /, so the compiler takes them as if they were C++ expressions, sometimes it can cause compilation to fail or create errors messages.

Using of namespace STD

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

If 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. Using of declaration is the typical way to introduce visibility of these components.

using namespace std;

Above declaration permits all elements in the STD namespace to be accessed in an unqualified manner.

With this in mind, the last example can be rewritten to make unqualified uses of cout as:

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

#include <iostream>

using namespace std;

int main ()

 

{

cout << “Hello World! “;

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

}

Hello World! I’m a C++ program

 

Conclusion

The two ways are accessing the elements of the STD namespace are valid in C++, we hope if keep on learning from our notes so we guarantee that neither neither mistakes nor collision will happen. We hope that you will learn enough about C++ from this our notes and article.

Share.

About Author

Leave A Reply