Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

C++

Learn step by step



Structure of a program

Writing programs is the best way to learn a programming language. Given a simple program, it contains all fundamental components of a C++ programs:


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

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

Output

Hello World!

Let us examine above program line by line:

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

These are comments in the program, they make no effects to the program and are used by the programmer for short explanation.

Line 2: #include <iostream>:

Lines beginning with a sign hash(#) are directives read and interpreted by the preprocessor.
They are the special lines interpreted before compilation of the program.
In the program, #include, instructs preprocessor to include header file iostream.h, that allows to perform standard input and output operations.

Line 3: A blank line.:

They improve readability of the code. Blank lines have no effect on the program.

Line 4: int main ():

Line initiates the declaration of function. Essentially, a function is a group of code statements which are given a name: in this case, this gives the name "main" to the group of code statements that follow.

Function named main is a special function in all C++ programs; it is the function called by default when the program run's. The execution of all C++ programs begins with the main function and end with the end of main.

Lines 5 and 7: { and }:

The opening and closing braces({ }) defines beginning and end of the main. Everything between them is the main function body that defines what happens when the main function is called. All functions use a pair of braces to indicate the beginning and end of their definitions/body.

Line 6: std::cout << "Hello World!";:

This line is a C++ statement. Statement is an expression that can actually produce some effect. It is the meat of the program, specifying its actual behavior. Statements are executed in the order that they appear within a function's body

This statement has 3 parts: 1st is std::cout, It identifies standard character output (generally, the computer screen). 2nd, Insertion operator (<<), It indicates that what follows is inserted into std::cout. Finally, a string enclosed within quotes ("Hello world"), that is the content inserted into the standard output(std::out).

Notice that the statement ends with a semicolon (;). This character marks the end of any statement, just like the period(.) ends a sentence in English. All C++ statements must end with a semicolon. One of the most common syntax errors in C++ is forgetting to end the statement with a semicolon.


Comments

C++ supports two ways of commenting code:

1. // line comment
2. /* block comment */

Line comment: It discards everything from where the pair of slash signs (//) are found up to the end of that line. But block comments discards everything between the characters /* and the characters */ it can contain multiple lines.


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

Output:

Hello World! I'm a C++ program

Using namespace std

In C++ code before, we have seen cout being used instead of std::cout. The 1st one uses its unqualified name (cout), while the 2nd qualifies it directly with the namespace std (as std::cout).

cout is part of the C++ standard library, and all the elements in the standard C++ library are declared within a namespace named std.

common way of using these components is by means of using declarations:

using namespace std;

This allows all elements in the std namespace to be accessed in an unqualified manner (without the std:: prefix).

With this in mind, the last example can be rewritten as:


// my second program in C++
#include <iostream>
using namespace std;

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

Output

Hello World! I'm a C++ program

These are the ways of accessing the elements of the std namespace (explicit qualification and using declarations) and both are valid in C++ and produce the exact same behavior.



Related Videos