Structure Of Program C++

The best way to learn a programming language is by writing programs. Typically, the first program beginners write is a program called “Hello World”, which simply prints “Hello World” to your computer screen. Although it is very simple, it contains all the fundamental components C++ programs have:

// my first program in C++
#include <iostream>

int main()
{
  std::cout << "Hello World!";
}
Hello World!

You may have noticed that not all the lines of this program perform actions when the code is executed. There is a line containing a comment (beginning with //). There is a line with a directive for the preprocessor (beginning with #). There is a line that defines a function (in this case, the main function). And, finally, a line with a statements ending with a semicolon (the insertion into cout), which was within the block delimited by the braces ( { } ) of the main function.

The program has been structured in different lines and properly indented, in order to make it easier to understand for the humans reading it. But C++ does not have strict rules on indentation or on how to split instructions in different lines. For example, instead of

int main ()
{
  std::cout << " Hello World!";
}

We could have written:

int main () { std::cout << "Hello World!"; }

all in a single line, and this would have had exactly the same meaning as the preceding code. Now, let’s add an additional statement to our first program:

// 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

In this case, the program performed two insertions into std::cout in two different statements. Once again, the separation in different lines of code simply gives greater readability to the program, since main could have been perfectly valid defined in this way:

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

The source code could have also been divided into more code lines instead:

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

And the result would again have been exactly the same as in the previous examples.

www.000webhost.com