Namespaces 1 (cout, cin, STD)

1.  Namespaces

Before we start calculating and writing complex programs in C++ we should discuss about namespaces. So what is namespace? By the definition the namespace is a container for a set of identifiers (names) and allows the disambiguation of homonym identifiers in different namespaces. Namespaces usually group names based on their functionality. A name in a namespaces consists of a namespace identifier and a local name. The namespaces name are usually applied a prefix to the local name.  
In computer science a namespace (sometimes also called a name scope) is an abstract container or environment created to hold a logical grouping of unique identifiers or symbols (i.e., names). An identifier defined in a namespace is associated only with that namespace. The same identifier can be independently defined in multiple namespaces. That is, the meaning associated with an identifier defined in one namespace may or may not have the same meaning as the same identifier defined in another namespace. Languages that support namespaces specify the rules that determine to which namespace an identifier (not its definition) belongs
This concept can be illustrated with an analogy. Imagine that two companies, X and Y, each assign ID numbers to their employees. X should not have two employees with the same ID number, and likewise for Y; but it is not a problem for the same ID number to be used at both companies. For example, if Bill works for company X and Jane works for company Y, then it is not a problem for each of them to be employee #123. In this analogy, the ID number is the identifier, and the company serves as the namespace. It does not cause problems for the same identifier to identify a different person in each namespace.
In large computer programs or documents it is not uncommon to have hundreds or thousands of identifiers. Namespaces (or a similar technique, see Emulating namespaces) provide a mechanism for hiding local identifiers. They provide a means of grouping logically related identifiers into corresponding namespaces, thereby making the system more modular.
Data storage devices and many modern programming languages support namespaces. Storage devices use directories (or folders) as namespaces. This allows two files with the same name to be stored on the device so long as they are stored in different directories. In some programming languages (e.g. C++, Python), the identifiers naming namespaces are themselves associated with an enclosing namespace. Thus, in these languages namespaces can nest, forming a namespace tree. At the root of this tree is the unnamed global namespace.
You are already familiar with the fact that all the names used in the ISO/IEC C++ standard library are defined in the namespaces with the name std. This means that all the names used in the standard library have an additional qualifying the name std. For example, cout is really std::cout. You have already seen how you can add a using declaration to import a name from the std namespace into your source file. For example: 

using std::cout;

This allows you to use the name cout in your source fi le and have it interpreted as std::cout . Namespaces provide a way to separate the names used in one part of a program from those used in another. This is invaluable with large projects involving several teams of programmers working on different parts of the program. Each team can have its own namespace name, and worries about two teams accidentally using the same name for different functions disappear.

Look at this line of code:
using namespace std;
This statement is a using directive and is different from a using declaration. The effect of this is to import all the names from the std namespace into the source fi le so you can refer to anything that is defined in this namespace without qualifying the name in your program. Thus, you can write the name cout instead of std::cout and endl instead of std::endl . This sounds like a big advantage, but the downside of this blanket using directive is that it effectively negates the primary reason for using a namespace — that is, preventing accidental name clashes. There are two ways to access names from a namespace without negating its intended effect. One way is to qualify each name explicitly with the namespace name; unfortunately, this tends to make the code very verbose and reduce its readability. The other possibility that I mentioned early on in this chapter is to introduce just the names that you use in your code with using declarations as you have seen in earlier examples, like this, for example:

using std::cout; // Allows cout usage without qualification

using std::endl; // Allows endl usage without qualification

Each using declaration introduces a single name from the specified namespace and allows it to be used unqualified within the program code that follows. This provides a much better way of importing names from a namespace, as you only import the names that you actually use in your program. Because Microsoft has set the precedent of importing all names from the System namespace with C++/CLI code, I will continue with that in the C++/CLI examples. In general, I recommend that you use using declarations in your own code rather than using directives when you are writing programs of any significant size. Of course, you can define your own namespace that has a name that you choose. 

1.1                 What is STD?

In the C++ programming language, the C++ Standard Library is a collection of classes and functions, which are written in the core language and part of the C++ ISO Standard itself. The C++ Standard Library provides several generic containers, functions to utilize and manipulate these containers, function objects, generic strings and streams (including interactive and file I/O), support for some language features, and everyday functions for tasks such as finding the square root of a number. The C++ Standard Library also incorporates 18 headers of the ISO C90 C standard library ending with ".h", but their use is deprecated. No other headers in the C++ Standard Library end in ".h". Features of the C++ Standard Library are declared within the std namespace.
The C++ Standard Library is based upon conventions introduced by the Standard Template Library (STL). Although the C++ Standard Library and the STL share many features, neither is a strict superset of the other. In particular, the C++ Standard Library has also been influenced by the work of Alexander Stepanov and Meng Lee.
The C++ Standard Library underwent ISO standardization as part of the C++ ISO Standardization effort, and is undergoing further work regarding standardization of expanded functionality.
Share on Google Plus