Structure of C++ program

Before discussing any special feature of C++,You must know almost enough of the basics to create and compile a Program. The programs written in C++ will use iostream classes. These read from and write to files and “standard” input and output. The first program to Print the Hello World Program

#include <iostream>
using namespace std;
int main ()
{
  cout << “Hello World”;
  return 0;
}

Output is:

Hello World

Let’s examine this program:

  • We first include the iostream header file that allows us to display output.
  • The cout object is defined inside the std namespace. To use the std namespace, we used the using namespace std; statement.
  • Every C++ program starts with the main() function. The code execution begins from the start of the main() function.
  • cout is an object that prints the string inside quotation marks ” “. It is followed by the << operator.
  • return 0; is the “exit status” of the main() function. The program ends with this statement, however, this statement is not mandatory.

Note: If we don’t include the using namespace std; statement, we need to use std::cout instead of cout.

 

What is a namespace? 

The namespace is a collection of software components with the same name prefix – e.g., std.
we need to mention the name space in each header file like

using namepace std;

The above statement tells that we are using the software components from the standard library