Namespace in C++: namespace keyword and examples

A namespace is used to create a declarative region. In this region plethora of program elements are placed. It is used to localize identifier names to avoid name mismatches. In C++ several same names are used for different purposes. Say, two functions can have the same name, yet they are performing different tasks altogether. If used in a regular way,  it would result in a name collision. This could be avoided by using the concept of namespace keyword. A namespace is used for localization and setting up a scope for a name. It comes in very handy to organize large programs. Elements in one namespace are different from elements in the other namespace.

Syntax of namespace:

namespace name {


For example:

using namespace std;

std is the namespace for C++ standard library.

Leave a Reply