Directives and Namespaces

Having just started with C++, I am higly confused with directives and namespaces.

This website defined Namespaces as things which "allow [one] to group entities like classes, objects and functions under a name." But what are they?

Also, I have a still don't understand this directive: "#include <iostream>". Could someone please expand on or clarify this statement:

"In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++"


What are the "declarations of the basic standard input-output library in C++?"

Many thanks! Please excuse my ignorance :)
A namespace is used to avoid name collision or to group something
eg:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
namespace hello
{
    void function()
    {
        cout << "HELLO";
    }
}
namespace world
{
    void function()
    {
        cout << "WORLD";
    }
}

You have two functions with the same name but you won't have problems as if you want to call one of them you sould specify the namespace:
1
2
hello::function();
world::function();



the #include directive copies the content of a file in the sourcecode in which is called.
eg:
file: declaration.h
1
2
3
4
5
//some functions

int function(int);

int otherfunction();

file: main.cpp
1
2
3
4
5
6
#include "declaration.h"

int main()
{
    function( 10 );//You can use the function as if it was declared in this file
}


http://www.cplusplus.com/doc/tutorial/preprocessor.html
http://www.cplusplus.com/doc/tutorial/namespaces.html


Last edited on
Topic archived. No new replies allowed.