C++: What is a header, and a namespace !

 Objectives :
  • In this example we use a namespace, knows as std
  • Headers and directives are explained: Link
#include <iostream>
using namespace std;
//first_namespace.cpp
int main()
{
std::cout << "Welcome to C++!\n";
cout <<"seondline ";
return 0;
}
 
A header file, for example  #include <iostream>, commonly contains declarations of various identifiers and sometimes executable statements. The intent is that in a common scenario the identifiers that need to be declared  more than once in a source file can be placed in one header file, which is then referred by the source codes whenever its contents are required.
  • // Option 1: referencing an object with :: operator every time in the code 
    #include <iostream>
    int main()
    {
       std::cout << "Hello, world!" << std::endl;
    }
  • // Option 2: with the key word "using" in the header section we can use those objects with their extensions.
    #include <iostream>
    using std::cout;
    using std::endl;
    int main()
    {
       cout << "Hello, world!" << endl;
    }
  • // Option 3 here std::cout and endl are bundled up in a namespace
    #include <iostream>
    using namespace std;
    int main()
    {
       cout << "Hello, world!" << endl;
    }