The best way to start learning a programming language is by writing a program. A simple C++ program has four sections and these are shown in following C++ program

Simple C++ Program:

#include                                  // Section: 1- The include Directive 

using namespace std;             // Section :2 - Class declaration and member 


int main ()                           // Section: 3 - Main function definition 

{                                         // Section: 4 - Declaration of an object 

     cout << "Hello World!"; 

     return 0; 


Hello World! 

This is one of the simplest programs that can be written in C+ programming language. It contains all the fundamental components which every C++ program can have. Line by line explanation of the codes of this program and its sections is given below:

Section: 1 – The include Directive 

#include <iostream.h>

Lines beginning with a hash sign (#) are directives for the pre-processor. They are not regular code lines with expressions but indications for the compiler's pre-processor. In this case the directive #include tells the pre-processor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program. 

Section: 2 – Class declaration and member functions 

 using namespace std; 

All the elements of the standard ANSI C++ library are declared within namespace std;. The syntax of this command is: using namespace std;. In order to access its functionality we declare all the entities inside namespace std;. This line is very often used in C++ programs that use the standard library and defines a scope for the identifiers that are used in a program.

Section: 3 - Main function definition 

int main () 

 This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function. 

The word main is followed in the code by a pair of parentheses (()). That is because it is a function declaration: In C++, what differentiates a function declaration from other types of expressions is these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within-them. 

Section: 4 - Declaration of an object 

Right after these parentheses we can find the body of the main function enclosed in braces ({}). What is contained within these braces is what the function does when it is executed. 

cout << "Hello World!"; 

 This line is a C++ statement. A statement is a simple or compound expression that can actually produce some effect. In fact, this statement is used to display output on the  screen of the computer. cout is the name of the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters. cout is declared in the iostream standard file within the std namespace, so that's why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code. 

Notice that the statement ends with a semicolon character (;). This character is used to mark the end of the statement and in fact it must be included at the end of all expression statements in all C++ programs. 

return 0; 

 The return statement causes the main function to finish.


"C++ programs examples with output"

"C++ programs examples "

"C++ basic programs for practice"

"C++ program to add two numbers"

"C++ programs for beginners"

"C++ code examples advanced"