{"id":215,"date":"2018-07-11T05:23:49","date_gmt":"2018-07-11T05:23:49","guid":{"rendered":"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=215"},"modified":"2018-07-30T10:28:49","modified_gmt":"2018-07-30T10:28:49","slug":"namespace-in-c","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/chapter\/namespace-in-c\/","title":{"rendered":"Namespace in C++"},"content":{"raw":"<strong>Introduction<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In C++, identifiers may have either local or global scope. C++ allows unique names in specific scope. When C++ application uses libraries from different vendors or when application is developed using multiple source files written by various programmers, there are chances of having identifiers with same name in global scope namespace. This introduces a problem of name conflict. Namespace helps to prevent such name conflict by allowing identifiers to be grouped in a user-defined namespace.<\/p>\r\n&nbsp;\r\n\r\nWhat is namespace?\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Namespace is a declarative region providing a scope to the identifiers contained in it. The identifiers are named entities, such as variable, function, class, object, typename, struct, enum or other compound types. Identifiers should have unique names in a namespace.<\/p>\r\n&nbsp;\r\n\r\nWhy namespace?\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Before understanding the need of namespace, let us recall the knowledge about global and local scope of variables. Namespace is sometimes referred to as scope also.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Named entities, such as variables, functions, class need to be declared before being used in C++. Depending upon the location of their declaration in a program, entities are having their scope or visibility.<\/p>\r\n&nbsp;\r\n\r\n<strong>1.1. Local namespace<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">An entity declared within a block has <em>block scope<\/em>. Block may be introduced using statements in a pair of curly braces. Some examples of blocks are functions, loops, conditions. Parameters of a function and entities declared within a block are local to the block. Such entities are visible only within the specific block in which they are declared, but not outside it. Their storage class is auto; i.e. they are created when the block is executed and their life is till the end of block. Variables with block scope are known as <em>local variables<\/em>. They are not initialized with any default value.<\/p>\r\n&nbsp;\r\n\r\nLocal variables are in local namespace.\r\n\r\n&nbsp;\r\n\r\n<strong>1.2. Global namespace<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">An entity declared outside any block has <em>global scope<\/em>, meaning that its name is valid anywhere in the code. Storage for global entities is static. Their life is for the entire duration of the program. Variables with global scope are known as global variables. By default, variables with <em>static storage<\/em> are initialized to zeroes, so global variables have zero as their initial value.<\/p>\r\n&nbsp;\r\n\r\nGlobal variables are in global namespace.\r\n\r\n&nbsp;\r\n\r\n<strong>1.3. Unique name in namespace<\/strong>\r\n\r\n&nbsp;\r\n\r\nIn each scope, may be block (local) or global, a name can represent only one entity. Thus, there cannot be two variables with the same name in the same scope.\r\n\r\n&nbsp;\r\n\r\nint function1 ()\r\n\r\n{\r\n\r\nint x;\r\n\r\n&nbsp;\r\n\r\nx = 0;\r\n\r\n&nbsp;\r\n\r\ndouble x; \/\/ wrong: name already used in this scope x = 0.0;\r\n\r\n}\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Blocks may be nested. The visibility of an entity with <em>block scope<\/em> extends until the end of the block, including inner blocks. An inner block, being a different block, can re-utilize a name existing in an outer block scope to refer to a different entity. In such case, the name will refer to a different entity within the inner block, hiding the entity with the same name in outer block scope.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the following example, note that variable y of outer block has a scope in nested block also. There is no variable named y in inner block. But variable named x occurs in outer block as well as in inner block. Re-utilizing variable name x in inner block hides variable x of outer block. See global variable named x declared outside block. To refer it within block, it requires using scope resolution operator.<\/p>\r\n<img class=\"size-full wp-image-216 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-83.png\" alt=\"\" width=\"787\" height=\"379\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\">cout &lt;&lt; \"x: \" &lt;&lt; x &lt;&lt; '\\n';<\/p>\r\n<p style=\"text-align: center\">cout &lt;&lt; \"y: \" &lt;&lt; y &lt;&lt; '\\n';<\/p>\r\n<p style=\"text-align: center\">}<\/p>\r\n<p style=\"text-align: center\">cout &lt;&lt; \"outer block:\\n\";<\/p>\r\n<p style=\"text-align: center\">cout &lt;&lt; \"x: \" &lt;&lt; x &lt;&lt; '\\n';<\/p>\r\n<p style=\"text-align: center\">cout &lt;&lt; \"y: \" &lt;&lt; y &lt;&lt; '\\n';<\/p>\r\n<p style=\"text-align: center\">cout &lt;&lt; \"global variable x: \" &lt;&lt; ::x &lt;&lt; '\\n';<\/p>\r\n<p style=\"text-align: center\">return 0;<\/p>\r\n<p style=\"text-align: center\">}<\/p>\r\n<strong>1.4. Need of namespace<\/strong>\r\n\r\n&nbsp;\r\n\r\nAs explained, only one entity can exist with a particular name in a particular scope or namespace in C++.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">For local variables in block scope, there is hardly a chance for entities with same name; because blocks tend to be relatively short and names have particular purposes within them. But, for global scope, chances are very high for having multiple entities with same name as explained in following scenario.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Real applications are usually very large and divided into multiple modules or tasks. These modules are mostly developed by more than one developer in an independent manner. It is possible that different developers used same variable name or class name for different purposes in their programs. To produce the final application, these separate source files are compiled and linked. Application will not be created successfully due to conflict in names. Error will occur considering re-declaration of a variable.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">For example, consider an event management application consisting of many program files. Assume one file having variable \u2018length\u2019 referring to number of days of an event. In another file, variable \u2018length\u2019 is used to store number of characters used in the string representing event name. While linking these two files, problem will arise due to name clash in variable \u2018length\u2019.<\/p>\r\n&nbsp;\r\n\r\nMany times, application may include libraries developed by different vendors. Hence, it is possible to have entities with same name in different libraries.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">For example, assume library1 containing class Angle in degree and library2 containing class Angle in radian. When these two libraries are included in a program, occurrence of repeated entity name Angle will create a conflict.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">C++ do not allow two entities with the same name in the same scope. Entities declared in separate files or libraries share global namespace. Hence, multiple definitions of names or name clashes may be encountered while linking the separate modules.<\/p>\r\n&nbsp;\r\n\r\nTo solve this problem, some workarounds may be like\r\n<ul>\r\n \t<li>vendors or developers should cooperate with some strategy to have unique names<\/li>\r\n \t<li>use dummy classes or structs to group names<\/li>\r\n<\/ul>\r\nC++ provides a better solution with namespace mechanism to overcome the problem of name clashes in the global scope.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The namespace mechanism allows global scope to be partitioned into number of sub-scopes, each sub-scope having its own name. In essence, a namespace defines a user-defined scope. User can create namespace to hold logical grouping of unique identifiers. An identifier defined in a namespace is associated only with that namespace. Thus, identifiers of one namespace do not conflict with identifiers in another user-defined namespace or global namespace.<\/p>\r\n&nbsp;\r\n\r\nThus, namespace provides a method for preventing name conflicts in large projects.\r\n\r\n&nbsp;\r\n\r\nUsing namespace, one can define the context in which names are defined.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Namespaces <\/strong>are used to organize code into logical groups and prevent name collisions that can occur especially when the code base includes multiple libraries.<\/p>\r\n&nbsp;\r\n\r\nUsing namespace\u00a0 \u00a0Using namespace involves:\r\n<ul>\r\n \t<li>Creating namespace with unique identifiers as its members or elements<\/li>\r\n \t<li>Accessing elements or members of namespace<\/li>\r\n<\/ul>\r\n<strong>1.5. Creating Namespace<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe syntax to declare a namespaces is: namespace namespace_name\r\n\r\n&nbsp;\r\n\r\n{ \/\/namespace body\r\n<ul>\r\n \t<li>\/\/ identifier declarations<\/li>\r\n<\/ul>\r\n}\r\n<ul>\r\n \t<li>A namespace definition begins with the keyword <strong>namespace<\/strong> followed by the user-defined name of namespace and its body in a pair of curly brackets.<\/li>\r\n \t<li>Body part contains identifiers as its members having unique name. Identifiers may be variables, constants, functions, class, objects of class etc.<\/li>\r\n \t<li>Namespace definition doesn't terminate with a semicolon.<\/li>\r\n \t<li>Function or class can be declared in namespace and defined outside namespace.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\nNote that a namespace definition must appear either at file scope, or immediately within another namespace definition. It cannot be defined at block scope, so cannot be defined in main(). Following example illustrates declaring variable, function, class and object in a namespace named myNamespace.\r\n\r\n&nbsp;\r\n\r\nnamespace myNamespace\r\n\r\n{\r\n\r\nint num = 5; \/\/ variable declaration\r\n\r\n&nbsp;\r\n\r\nint isLeap(int y) \/\/ non-member function declaration\r\n\r\n{\r\n\r\nif ( (y%400 ==0) || (y%4==0 &amp;&amp; y%100 !=0) ) return 1; else return 0;\r\n\r\n}\r\n\r\nclass Point\u00a0\u00a0\u00a0\u00a0 \/\/ class declaration\r\n\r\n{\r\n\r\nfloat x,y;\r\n\r\npublic:\r\n\r\nPoint() {x = y = 0;}\r\n\r\nPoint(float p1, float p2) {x=p1; y=p2;}\r\n\r\nvoid show() {cout &lt;&lt; '(' &lt;&lt; x &lt;&lt; ',' &lt;&lt; y &lt;&lt; ')';} }; \/\/ end class\r\n\r\nPoint pt; \/\/ object declaration\r\n\r\n&nbsp;\r\n\r\n} \/\/ end myNamespace\r\n\r\n&nbsp;\r\n\r\n<strong>Function and class can be declared inside the namespace and defined outside it <\/strong>using scope resolution operator as follows:\r\n\r\n&nbsp;\r\n\r\nnamespace myNamespace\r\n\r\n{\r\n\r\nint num = 5; \/\/ variable declaration\r\n\r\nint isLeap(int y); \/\/ non-member function declaration\r\n\r\nclass Point;\u00a0\u00a0\u00a0\u00a0 \/\/ class declaration\r\n\r\n\/\/Point pt; \/\/ object declaration, error:'pt' uses undefined class\r\n\r\n'myNamespace::Point'\r\n\r\n} \/\/ end myNamespace\r\n\r\n&nbsp;\r\n\r\n\/\/defining function and class outside namespace using scope resolution operator int myNamespace::isLeap(int y) \/\/ non-member function definition\r\n\r\n{\r\n\r\nif ( (y%400 ==0) || (y%4==0 &amp;&amp; y%100 !=0) ) return 1; else return 0;\r\n\r\n}\r\n\r\nclass myNamespace::Point\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ class definition outside namespace\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nfloat x,y;\r\n\r\npublic:\r\n\r\nPoint() {x = y = 0;}\r\n\r\n&nbsp;\r\n\r\nPoint(float p1, float p2) {x=p1; y=p2;}\r\n\r\n&nbsp;\r\n\r\nvoid show() {cout &lt;&lt; '(' &lt;&lt; x &lt;&lt; ',' &lt;&lt; y &lt;&lt; ')';} }; \/\/ end class\r\n\r\n&nbsp;\r\n\r\nIt is possible to define <strong>class in namespace with its methods defined outside<\/strong> <strong>namespace <\/strong>as follows:\r\n\r\nnamespace myNamespace\r\n\r\n{\r\n\r\nint num = 5; \/\/ variable declaration\r\n\r\nint isLeap(int y); \/\/ non-member function declaration class Point \/\/ class definition\r\n\r\n{\r\n\r\nfloat x,y;\r\n\r\npublic:\r\n\r\nPoint() {x = y = 0;}\r\n\r\nPoint(float p1, float p2) {x=p1; y=p2;}\r\n\r\nvoid show(); \/\/ prototype declaration\r\n\r\n}; \/\/ end class\r\n\r\n&nbsp;\r\n\r\n} \/\/ end myNamespace\r\n\r\n&nbsp;\r\n\r\n\/\/ function defined outside namespace\r\n\r\nint myNamespace::isLeap(int y) \/\/ non-member function definition\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nif ( (y%400 ==0) || (y%4==0 &amp;&amp; y%100 !=0) ) return 1; else return 0;\r\n\r\n}\r\n<ul>\r\n \t<li>\/\/ class method show() defined outside class, outside namespace void myNamespace::Point::show() \/\/ definition<\/li>\r\n<\/ul>\r\n{cout &lt;&lt; '(' &lt;&lt; x &lt;&lt; ',' &lt;&lt; y &lt;&lt; ')';}\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>1.6. Accessing elements of namespace<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">All identifiers within the scope are directly accessible without qualification. But outside the namespace scope, identifiers can be accessed by using the fully qualified name, or by using <em>Declaration<\/em> for a single identifier, or by using <em>Directive<\/em> for all the identifiers in the namespace. Thus there are three ways that code outside namespace can access their elements.<\/p>\r\n\r\n<ul>\r\n \t<li>Using fully qualified name with scope resolution operator ::<\/li>\r\n \t<li>Importing entire namespace using directive<\/li>\r\n \t<li>Importing single element at a time from namespace using declaration<\/li>\r\n<\/ul>\r\n<strong>1.6.1. Accessing namespace elements using scope resolution operator<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Any name (identifier) declared in a namespace can be explicitly referred using the namespace's name and the scope resolution operator with the identifier. This type of accessing name is also referred to as fully qualified name. Following example defines identifier named \u2018num\u2019 in global namespace and in user-defined namespace. Identifier of user-defined namespace is referred using fully qualified name.<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n#include &lt;iostream&gt;\r\n\r\nusing namespace std;\r\n\r\nint num=50; \/\/ global var\r\n\r\nnamespace myNamespace\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nint num = 5; \/\/ variable declaration\r\n\r\nint isLeap(int y) \/\/ non-member function definition\r\n\r\n{\r\n\r\nif ( (y%400 ==0) || (y%4==0 &amp;&amp; y%100 !=0) ) return 1; else return 0;\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\nclass Point\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ class definition\r\n\r\n{\r\n\r\nfloat x,y;\r\n\r\n&nbsp;\r\n\r\npublic:\r\n\r\nPoint() {x = y = 0;}\r\n\r\nPoint(float p1, float p2) {x=p1; y=p2;}\r\n\r\n&nbsp;\r\n\r\nvoid show() {cout &lt;&lt; '(' &lt;&lt; x &lt;&lt; ',' &lt;&lt; y &lt;&lt; ')';} }; \/\/ end class\r\n\r\nPoint pt; \/\/ object declaration\r\n\r\n} \/\/ end myNamespace\r\n\r\n&nbsp;\r\n\r\nint main()\r\n\r\n{\r\n\r\ncout &lt;&lt; \"Global scope num: \" &lt;&lt; num &lt;&lt; endl;\r\n\r\ncout &lt;&lt; \"myNamespace scope num: \" &lt;&lt; myNamespace::num &lt;&lt; endl;\r\n\r\ncout &lt;&lt; \"Enter year:\";\r\n\r\ncin &gt;&gt; num;\r\n\r\nif (myNamespace::isLeap(num)) cout &lt;&lt; num &lt;&lt; \" is a leap year\" &lt;&lt; endl;\r\n\r\nelse cout &lt;&lt; num &lt;&lt; \" is not a leap year\" &lt;&lt; endl;\r\n\r\nmyNamespace::pt.show();\r\n\r\nmyNamespace::Point pt1(3.5, 4.5);\r\n\r\npt1.show();\r\n\r\nreturn 0;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>Output:<\/strong>\r\n\r\n&nbsp;\r\n\r\nGlobal scope num: 50\r\n\r\nmyNamespace scope num: 5\r\n\r\nEnter year:1900\r\n\r\n1900 is not a leap year\r\n\r\n&nbsp;\r\n\r\n(0,0)(3.5,4.5)\r\n\r\n&nbsp;\r\n\r\nRefer following example having three variables with same name; each in global, local and user-defined namespace scope.\r\n<ul>\r\n \t<li>\/\/ namescope element access with qualified name<\/li>\r\n \t<li>\/\/ 3 variables with same name but in different scopes #include &lt;iostream&gt;<\/li>\r\n<\/ul>\r\nusing namespace std;\r\n\r\n&nbsp;\r\n\r\nint num=50; \/\/ global variable\r\n\r\nnamespace myFirstNamespace\r\n\r\n&nbsp;\r\n\r\n{\u00a0 int num = 5; }\r\n\r\n&nbsp;\r\n\r\nint main()\r\n\r\n{\r\n\r\nint num = 70; \/\/ block scope (local variable) cout &lt;&lt; \"Global scope num: \" &lt;&lt; ::num &lt;&lt; endl; cout &lt;&lt; \"Block scope num: \" &lt;&lt; num &lt;&lt; endl;\r\n\r\ncout &lt;&lt; \"myFirstNamespace scope num: \" &lt;&lt; myFirstNamespace::num &lt;&lt; endl; return 0;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>Output:<\/strong>\r\n\r\nGlobal scope num: 50\r\n\r\nBlock scope num: 70\r\n\r\nmyFirstNamespace scope num: 5\r\n\r\nIdentifiers will be looked up first in local namespace. So, global declarations with the same name can be accessed using scope resolution (::) .\r\n\r\n&nbsp;\r\n\r\n<strong>1.6.2. Importing namespace elements with keyword \u2018using\u2019<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The <strong>using<\/strong> keyword can be used to import entire or part of namespace into current declarative region.<\/p>\r\n<p style=\"text-align: justify\">Names introduced into a namespace scope with \u2018using\u2019 can be used with or without qualification from other scopes.<\/p>\r\n<p style=\"text-align: justify\">When we use <strong>using directive<\/strong>, we import all the names in the namespace. But with <strong>using declaration<\/strong>, we import one specific name at a time.<\/p>\r\n<p style=\"text-align: justify\">Namespace can be introduced to another named namespace or to a local block scope or file scope in a program depending upon where \u2018using\u2019 is introduced.<\/p>\r\n<p style=\"text-align: justify\">Names introduced with \u2018using\u2019 keyword obey normal scope rules. The name is visible from the point of the \u2018using\u2019 to the end of the scope in which the directive\/declaration is found.<\/p>\r\n<p style=\"text-align: justify\">Entities with the same name defined in an outer scope are hidden.<\/p>\r\n&nbsp;\r\n\r\n<strong>1.7. Examples to understand use of directive and declaration at various scopes<\/strong>\r\n\r\n&nbsp;\r\n\r\nSyntax for importing entire namespace with \u2018using directive\u2019:\r\n\r\nusing namespace &lt;namespace_name&gt; Syntax for importing single element from namespace with \u2018using declaration\u2019:\r\n\r\nusing &lt;namespace_name::element_name&gt;\r\n\r\nNote that keyword \u2018namespace\u2019 is not used in \u2018using declaration\u2019.\r\n\r\n&nbsp;\r\n\r\n<strong>1.7.1. Using Directive at block (local) scope<\/strong>\r\n\r\n&nbsp;\r\n\r\nFollowing example shows importing all names from namespace using directive at block scope. Within block, namespace identifiers are referred without qualification.\r\n<ul>\r\n \t<li>\/\/ importing entire namespace using directive<\/li>\r\n \t<li>\/\/ importing namespace at block scope #include &lt;iostream&gt;<\/li>\r\n<\/ul>\r\nusing namespace std; namespace first\r\n\r\n&nbsp;\r\n\r\n{ int x = 5; int y = 10;} namespace second\r\n\r\n{ double x = 3.1416; int y = 20; }\r\n\r\nint main()\r\n\r\n{\r\n\r\n{ \/\/ block1\r\n\r\nusing namespace first; \/\/ import all identifiers of first in current block\u2019s scope cout &lt;&lt; x &lt;&lt; ', ' &lt;&lt; y &lt;&lt; '\\n';\r\n\r\n}\r\n\r\n{\/\/ block2\r\n\r\nusing namespace second; \/\/ import all identifiers of second in current block\u2019s scope\r\n\r\ncout &lt;&lt; x &lt;&lt; ', ' &lt;&lt; y &lt;&lt; '\\n';\r\n\r\n}\r\n\r\nreturn 0;\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>Output:<\/strong>\r\n\r\n&nbsp;\r\n\r\n5, 10\r\n\r\n3.1416, 20\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Removing blocks from \u2018main\u2019 in above code will raise ambiguity in names x and y when elements of namespace second are accessed. Note that variables x and y are not redefined here, they are just imported to be in current scope. So, error is not flagged at the time of importing with using directive.<\/p>\r\n&nbsp;\r\n\r\nint main()\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nusing namespace first; \/\/ import all identifiers of first in current block\u2019s scope cout &lt;&lt; x &lt;&lt; ',' &lt;&lt; y &lt;&lt; '\\n';\r\n\r\nusing namespace second; \/\/ import all identifiers of second in current block\u2019s scope\r\n\r\ncout &lt;&lt; x &lt;&lt; ',' &lt;&lt; y &lt;&lt; '\\n'; \/\/ ambiguous x and y\r\n\r\n}\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">To get rid of error due to ambiguity in names, use fully qualified name like second::x and second::y. Note that it is always safer to use fully qualified name.<\/p>\r\n<p style=\"text-align: justify\">Note that identifiers of local namespace hide identical names of namespace even when directive is used. Try defining int x = 555 in main() and see the result.<\/p>\r\n<strong>1.7.2. Using Directive at file scope (global within single source file)<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">When \u2018using namespace\u2019 or \u2018using\u2019 is used outside any block like global variables, its elements have file scope. It means that such identifiers are visible only in this specific file; they are not available in other files. This is how it differs from normal global scope. Variables defined in global namespace can be availed in other files by declaring them as \u2018extern\u2019. Identifiers of namespace cannot be declared as \u2018extern\u2019. If needed in another file, they need to be imported with \u2018using\u2019 keyword.<\/p>\r\n&nbsp;\r\n<ul>\r\n \t<li>\/\/ importing entire namespace using directive<\/li>\r\n \t<li>\/\/ importing namespace at file (global in file) scope #include &lt;iostream&gt;<\/li>\r\n<\/ul>\r\nusing namespace std;\r\n\r\n&nbsp;\r\n\r\nnamespace first\r\n\r\n&nbsp;\r\n\r\n{ int x = 5; int y = 10;} namespace second\r\n\r\n{ double x = 3.1416; int y = 20; }\r\n\r\n&nbsp;\r\n\r\nusing namespace first; \/\/ import all identifiers of namespace first here, at file scope\r\n\r\n&nbsp;\r\n\r\nint main ()\r\n\r\n{\r\n\r\ncout &lt;&lt; \"from first namespace: \" &lt;&lt; x &lt;&lt; \", \" &lt;&lt; y &lt;&lt; '\\n';\r\n\r\n&nbsp;\r\n\r\ncout &lt;&lt; \"from second namespace: \" &lt;&lt; second::x &lt;&lt; \", \" &lt;&lt; second::y &lt;&lt; '\\n'; return 0;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>Output:<\/strong>\r\n\r\n&nbsp;\r\n\r\nfrom first namespace: 5, 10\r\n\r\nfrom second namespace: 3.1416, 20\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">When identifiers in global namespace collide with identifiers in namespace scope, it results in compile time error. Adding int x; before main() in above code will give an error due to ambiguity at the point where x is used. Error is not flagged at the point of defining x with the same name or at the time of importing namespace.In this example, what will happen if both namespaces are imported at global or file scope here? Will it create name conflict problem?<\/p>\r\n&nbsp;\r\n\r\n<strong>1.7.3. Using Directive at another namespace scope<\/strong>\r\n<ul>\r\n \t<li>\/\/ importing entire namespace with using directive into another namespace #include &lt;iostream&gt;<\/li>\r\n<\/ul>\r\nusing namespace std;\r\n\r\nnamespace second\r\n\r\n{ double x = 3.1416; int y = 20; } namespace third\r\n\r\n{ using namespace second; \/\/ import namespace second in namespace third double xx = 7.14; int yy = 50; }\r\n\r\nusing namespace third; \/\/ import namespace third here, global scope\r\n\r\nint main ()\r\n\r\n{\r\n\r\ncout &lt;&lt; \"from third namespace: \" &lt;&lt; x &lt;&lt; \", \" &lt;&lt; y\r\n<ul>\r\n \t<li>&lt;&lt; \", \" &lt;&lt; xx &lt;&lt; \", \" &lt;&lt; yy &lt;&lt; '\\n'; return 0;<\/li>\r\n<\/ul>\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>Output:<\/strong>\r\n\r\n&nbsp;\r\n\r\nfrom third namespace: 3.1416, 20, 7.14, 50\r\n\r\n&nbsp;\r\n\r\n<strong>1.7.4. Using declaration at various scopes<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2018Using declaration\u2019 enables to import only one element of namespace at a time in current scope where it is used. Refer following example where a single element y of namespace first is imported at global scope. As seen in previous examples, it can be imported at block scope or at another namespace scope also.<\/p>\r\n\r\n<ul>\r\n \t<li>\/\/ importing single element at a time from namespace using declaration #include &lt;iostream&gt;<\/li>\r\n<\/ul>\r\nusing namespace std;\r\n\r\n&nbsp;\r\n\r\nnamespace first\r\n\r\n{ int x = 5; int y = 10;}\r\n\r\nusing first::y; \/\/ using declarative at global scope\r\n\r\nint x; \/\/ global variable\r\n\r\n&nbsp;\r\n\r\nint main ()\r\n\r\n{\r\n\r\nint x=50; \/\/ local variable\r\n<ul>\r\n \t<li>\/\/ global ::x; local x; first::x and y from namespace first cout &lt;&lt; ::x &lt;&lt; \", \" &lt;&lt; x &lt;&lt; \", \" &lt;&lt; first::x &lt;&lt; \", \" &lt;&lt; y &lt;&lt; '\\n'; return 0;<\/li>\r\n<\/ul>\r\n}\r\n\r\n&nbsp;\r\n\r\nOutput:\r\n\r\n&nbsp;\r\n\r\n0, 50, 5, 10\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In above example, importing first::x at global scope or block scope of main() will raise conflict as there is variable x with same name defined in the same scope. At global scope, it conflicts with global x; and at local scope, it conflicts with local x. In such cases, one may use \u2018using declaration\u2019 to import single element of namespace instead of importing all elements of namespace. Here, only element y of namespace first is imported in global scope.<\/p>\r\n&nbsp;\r\n\r\n<strong>1.7.5. Overriding name when same name is imported from different namespaces in same scope<\/strong>\r\n<ul>\r\n \t<li style=\"text-align: justify\">\/\/ importing entire namespace using directive and single element using declaration<\/li>\r\n \t<li style=\"text-align: justify\">\/\/ overriding name when same name is imported from namespace scope<\/li>\r\n<\/ul>\r\n#include &lt;iostream&gt;\r\n\r\n&nbsp;\r\n\r\nusing namespace std;\r\n\r\nnamespace first { int x = 5; int y = 10;}\r\n\r\nnamespace second { double x = 3.1416; int y = 20; }\r\n\r\nint main ()\r\n\r\n{\r\n\r\nusing namespace first; \/\/ x, y of first in block scope of main()\r\n\r\nusing second::x; \/\/ identifier x of namespace second overrides x of namespace first cout &lt;&lt; x &lt;&lt; \", \" &lt;&lt; y &lt;&lt; '\\n'; \/\/ refers to second::x, first::y\r\n\r\ncout &lt;&lt; first::x &lt;&lt; '\\n'; \/\/ x of first accessible using fully qualified name return 0;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>Output:<\/strong>\r\n\r\n&nbsp;\r\n\r\n3.1416, 10\r\n\r\n5\r\n<p style=\"text-align: justify\">Here, variable x of second namespace is imported after variables x and y of first namespace at block scope. Name imported later overrides already imported names. As seen in all above examples, <strong>using<\/strong> and <strong>using namespace<\/strong> have validity only in the same block in which they are stated or in the entire source code file if they are used directly in the global scope.<\/p>\r\n&nbsp;\r\n\r\n<strong>Namespace std<\/strong>\r\n\r\n&nbsp;\r\n\r\nAll the entities (variables, types, constants, and functions) of the standard C++ library are declared within the std namespace. Most examples used here include following line at file scope:\r\n\r\n&nbsp;\r\n\r\nusing namespace std;\r\n\r\n&nbsp;\r\n\r\nThis introduces direct visibility of all the names of the std namespace into the code. So, objects like cout and cin defined in std namespace are accessible without scope resolution operator.\u00a0 Without use of \u2018using namespace std\u2019, one will have to use std::cout and std::cin.\r\n\r\n&nbsp;\r\n\r\nFor example, instead of\r\n\r\ncout &lt;&lt; \u201cHello world\u201d;\r\n\r\nit requires writing\r\n\r\nstd::cout &lt;&lt; \u201cHello world\u201d;\r\n\r\n&nbsp;\r\n\r\nIncluding line \u2018using namespace std;\u2019 at file scope facilitate comprehension and shorten the length of the code. Many programmers prefer to qualify each of the elements of the standard library used in their programs.\r\n\r\n&nbsp;\r\n\r\nWhether the elements in the std namespace are introduced with using declarations or are fully qualified on every use, it does not change the behaviour or efficiency of the resulting program in any way. It is mostly a matter of style preference. Although for projects with mixing libraries, explicit qualification tends to be preferred.\r\n\r\n&nbsp;\r\n\r\n<strong>Anonymous Namespace<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">It is possible to create a namespace without giving any name. Such namespaces are called anonymous or unnamed namespaces.\u00a0Unnamed namespace can contain everything like named namespace. But, as it is not given any name, its elements cannot be accessed using qualified name or cannot be imported with \u2018using\u2019 keyword. Elements of unnamed namespace are available at file scope and can be accessed just like normal global variables within the file only.<\/p>\r\nRefer following example.\r\n\r\n\/\/ Unnamed namespace\r\n\r\n#include &lt;iostream&gt;\r\n\r\nusing namespace std;\r\n\r\nnamespace \/\/ anonymous, not given any name\r\n\r\n{ int x; int y; }\r\n\r\nint main ()\r\n\r\n{\r\n\r\ncout &lt;&lt; \"Enter two integres:\";\r\n\r\ncin &gt;&gt; x &gt;&gt; y;\r\n\r\ncout &lt;&lt; x &lt;&lt; \", \" &lt;&lt; y &lt;&lt; '\\n';\r\n\r\nreturn 0;\r\n\r\n}\r\n\r\nOutput:\r\n\r\nEnter two integres:5 10\r\n\r\n5, 10\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Even though variables x and y of anonymous namespace are accessed like normal global variables, they are not available in other files. They cannot be declared as \u2018extern\u2019 in other files.Unnamed namespace is unique within a file, i.e. there cannot be more than one unnamed namespace in a file. When unnamed space is defined again, it is considered as an extension to an existing unnamed space.<\/p>\r\n\r\n<ul>\r\n \t<li>\/\/ redefining unnamed space extends existing unnamed space #include &lt;iostream&gt;<\/li>\r\n<\/ul>\r\nusing namespace std;\r\n\r\n&nbsp;\r\n\r\nnamespace \/\/ anonymous, not given any name\r\n\r\n{ int x; int y; }\r\n\r\nnamespace \/\/ anonymous, extended\r\n\r\n{ int xx=10; int yy=20; } int main ()\r\n\r\n{\r\n\r\ncout &lt;&lt; \"Enter two integres:\"; cin &gt;&gt; x &gt;&gt; y;\r\n\r\ncout &lt;&lt; x &lt;&lt; \", \" &lt;&lt; y &lt;&lt; '\\n'; cout &lt;&lt; xx &lt;&lt; \", \" &lt;&lt; yy &lt;&lt; '\\n';\r\n\r\nreturn 0;\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>Output:<\/strong>\r\n\r\nEnter two integres:50 70\r\n\r\n&nbsp;\r\n\r\n50, 70\r\n\r\n10, 20\r\n\r\nWhat if a variable with the same name occurs at local and global scope? Refer following example.\r\n<ul>\r\n \t<li>\/\/ variables with same name in global, local and anonymous namespace scope #include &lt;iostream&gt;<\/li>\r\n<\/ul>\r\nusing namespace std;\r\n\r\n&nbsp;\r\n\r\nnamespace \/\/ anonymous, not given any name\r\n\r\n{ int x=5; int y=25; }\r\n\r\nnamespace \/\/ anonymous, not given any name\r\n\r\n{ int xx=10; int yy=20; } int x=55; \/\/global\r\n\r\nint main ()\r\n\r\n{\r\n\r\nint x=75; \/\/ local\r\n\r\ncout &lt;&lt; ::x &lt;&lt; \", \" &lt;&lt; x &lt;&lt; \", \" &lt;&lt; y &lt;&lt; '\\n';\r\n\r\ncout &lt;&lt; xx &lt;&lt; \", \" &lt;&lt; yy &lt;&lt; '\\n';\r\n\r\nreturn 0;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>Output:<\/strong>\r\n\r\n55, 75, 25\r\n\r\n&nbsp;\r\n\r\n10, 20\r\n\r\n&nbsp;\r\n\r\nIt can be observed that a variable in global scope is still available using scope resolution operator, but local variable hides the same name from anonymous namespace.\r\n\r\n&nbsp;\r\n\r\n<strong>Nested Namespace<\/strong>\r\n\r\n&nbsp;\r\n\r\nA namespace definition can be nested within another namespace definition. Every namespace definition must appear either at file scope or immediately within another namespace definition.\u00a0\u00a0Namespace can be nested up to any level.Elements of nested namespace can be accessed using qualified name and can also be imported with \u2018using\u2019.\r\n\r\n&nbsp;\r\n\r\nSee following example.\r\n\r\n&nbsp;\r\n\r\n#include &lt;iostream&gt;\r\n\r\nusing namespace std;\r\n\r\nnamespace namespaceOuter\r\n\r\n{\r\n\r\nfloat num1 = 10.5;\r\n\r\nnamespace namespaceInner1 \/\/nested\r\n\r\n{ float num1 = 25.25; float num2 = 30.75; }\r\n\r\n}\r\n\r\nint main ()\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n\/\/Accessing elements of nested namespace\r\n\r\n\/\/With qualified name using scope resolution operator cout &lt;&lt; \"outer num1:\" &lt;&lt; namespaceOuter::num1 &lt;&lt; endl;\r\n\r\ncout &lt;&lt; \"Inner num1:\" &lt;&lt; namespaceOuter::namespaceInner1::num1 &lt;&lt; endl; cout &lt;&lt; \"Inner num2:\" &lt;&lt; namespaceOuter::namespaceInner1::num2 &lt;&lt; endl;\r\n\r\n\/\/With \u2018using\u2019 keyword:\r\n\r\nusing namespace namespaceOuter::namespaceInner1;\r\n\r\ncout &lt;&lt; \" with using, inner num1 and num2...\" &lt;&lt; num1 &lt;&lt; \" \" &lt;&lt; num2 &lt;&lt; endl;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nOutput:\r\n\r\n&nbsp;\r\n\r\nouter num1:10.5\r\n\r\nInner num1:25.25\r\n\r\nInner num2:30.75\r\n\r\nwith using, inner num1 and num2...25.25 30.75\r\n\r\nNamespace Aliasing\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Namespace aliasing allow the programmer to define an alternate name for a namespace.It is commonly used as a convenient shortcut for long or deeply-nested namespaces.\u00a0Existing namespace can be aliased with new name, with the following syntax: namespace new_name = current_name; Aliasing helps to reduce the length of long fully qualified names: namespace ns1 = namespaceOuter::namespaceInner1; cout &lt;&lt; ns1::num1 &lt;&lt; ns1::num2;<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Aliasing also helps to get rid of changes in program code in case name of namespace is changed. Here, the purpose is somewhat similar to using #define. For example, if name of namespace namespaceInner1 is changed to namespaceLevel1, merely change in alias name helps in getting rid of changes wherever its elements are referred in a program.<\/p>\r\n&nbsp;\r\n\r\nnamespace ns1 = namespaceOuter::namespaceLevel1; \/\/ change in namespace name\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">cout &lt;&lt; ns1::num1 &lt;&lt; ns1::num2; \/\/ unaffected by name change due to alias<\/p>\r\n\/\/Namespace aliasing\r\n\r\n#include &lt;iostream&gt;\r\n\r\nusing namespace std;\r\n\r\nnamespace namespaceOuter\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nfloat num1 = 10.5;\r\n\r\nnamespace namespaceInner1 \/\/nested\r\n\r\n{ float num1 = 25.25; float num2 = 30.75; }\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nint main ()\r\n\r\n{\r\n<p style=\"text-align: justify\">namespace ns1 = namespaceOuter::namespaceInner1; \/\/ creating alias \/\/ using alias ns1 - ns1::num1, ns1::num2<\/p>\r\ncout &lt;&lt; \"Using alias - inner num1 and num2: \" &lt;&lt; ns1::num1 &lt;&lt; \" \" &lt;&lt;\r\n\r\nns1::num2 &lt;&lt; endl;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>Output:<\/strong>\r\n\r\n&nbsp;\r\n\r\nUsing alias - inner num1 and num2: 25.25 30.75\r\n\r\n&nbsp;\r\n\r\nExtending Namespace\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A namespace definition can be extended within the same file or over multiple files. This enables to split the namespace by declaring its elements in several parts as per need. So it can be referred to as discontinuous namespace also.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Extending namespace is useful when a large application is developed by more than one programmer. Programmers may define identifiers as per their individual need in the namespace and can be added in program code with other identifiers in the same namespace.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Syntax to extend namespace is same as creating namespace. Defining namespace for the first time creates new namespace. Redefining existing namespace simply extends it adding new elements.<\/p>\r\n&nbsp;\r\n\r\nNote: The namespace is not overwritten or overridden. It is either created or extended.\r\n\r\n&nbsp;\r\n\r\n<strong>Consider following examples:<\/strong>\r\n<ul>\r\n \t<li>\/\/ extending named namespace #include &lt;iostream&gt;<\/li>\r\n<\/ul>\r\nusing namespace std; namespace myNamespace { float num1 = 10.5; } namespace myNamespace \/\/ extended myNamespace { float num2 = 30.75; }\r\n\r\n&nbsp;\r\n\r\nusing namespace myNamespace;\r\n\r\nint main ()\r\n\r\n{\r\n\r\ncout &lt;&lt; \"myNamespace num1 and num2...\" &lt;&lt; num1 &lt;&lt; \" \" &lt;&lt; num2 &lt;&lt; endl; return 0;\r\n\r\n}\r\n\r\n<strong>Output:<\/strong>\r\n\r\n&nbsp;\r\n\r\nmyNamespace num1 and num2...10.5 30.75\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Consider following example of namespace extended after importing its elements. Here, namespace have two function members f(int) and f(char). The namespace is extended after the using-declaration of elements from existing namespace with member f(int). The namespace is extended but additional declarations f(char) with same name do not become visible through the using-declaration (in contrast with using-directive).<\/p>\r\n\r\n<ul>\r\n \t<li>\/\/ extending named namespace after using declaration #include &lt;iostream&gt;<\/li>\r\n<\/ul>\r\nusing namespace std;\r\n\r\n&nbsp;\r\n\r\nnamespace ns\r\n\r\n&nbsp;\r\n\r\n{ void f(int x) {cout &lt;&lt; x &lt;&lt; endl; } } using ns::f; \/\/ f now refers to ns::f(int)\r\n\r\n&nbsp;\r\n\r\nnamespace ns \/\/ namespace extension\r\n\r\n{ void f (char c) {cout &lt;&lt; c &lt;&lt; endl; } } \/\/ added f(char) in namespace ns\r\n\r\n&nbsp;\r\n\r\nint main ()\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nf('a'); \/\/ calls f(int), even though f(char) exists as f(char) not in scope ns::f('a'); \/\/ explicit qualification\r\n\r\nusing ns::f; \/\/ again using, now f is a synonym for both ns::f(int) and ns::f(char)\r\n\r\nf('a'); \/\/ calls f(char)\r\n\r\nf(65); \/\/ calls f(int)\r\n\r\nreturn 0;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>Output:<\/strong>\r\n\r\n&nbsp;\r\n\r\n97\r\n\r\na\r\n\r\na\r\n\r\n65\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nObserve following from the output:\r\n<ul>\r\n \t<li>first call f(\u2018a\u2019) calls f(int) as f(char) is not visible here and char can be implicitly converted to int.<\/li>\r\n \t<li style=\"text-align: justify\">In next line, call ns::f(\u2018a\u2019) with explicit qualification called f(char). It means f(char) is there in namespace, but it is yet not imported into current scope.With declaration \u2018using ns::f\u2019, now<\/li>\r\n \t<li style=\"text-align: justify\">f(char) is also in scope. So next calls f(\u2018a\u2019) and f(65) calls f(char) and f(int) respectively.<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">Modify above code to replace line \u2018using ns::f\u2019 with line \u2018using namespace ns\u2019 before extending namespace. See the effect. Now the output shows that it calls f(char), f(char), f(char) and f(int) displaying a, a, a and 65.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The point is: If the namespace is extended after the using-declaration of elements from existing namespace, the namespace is extended but additional declarations do not become visible through the using-declaration (in contrast with using-directive).<\/p>\r\n&nbsp;\r\n\r\nDo namespace introduce any overhead?\r\n\r\n&nbsp;\r\n\r\nNamespace does not introduce any overhead at runtime as namespaces are resolved at compile time. All fully qualified name and using directive or declarations are handled at compile time. Use of namespace does affect the readability of the program. So it should be used only when needed.\r\n\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n\r\n&nbsp;\r\n<ul>\r\n \t<li>Namespace is a feature added in C++ and not present in C<\/li>\r\n \t<li>A namespace is a declarative region that provides a scope to the identifiers (names of the types, function, variables etc) inside it<\/li>\r\n \t<li>C++ allows unique names in a scope. Namespace enables using same names in different context without name conflict<\/li>\r\n \t<li>The namespace definition must be done at global scope, or nested inside another namespace<\/li>\r\n \t<li>Its syntax is somewhat like class syntax, but<\/li>\r\n \t<li>Identifiers cannot have access specification (public, private, protected)<\/li>\r\n \t<li>Namespace definition doesn't terminate with a semicolon<\/li>\r\n \t<li>One cannot create instance of namespace<\/li>\r\n \t<li>Elements can be accessed using<\/li>\r\n \t<li>Qualification name with scope resolution operator<\/li>\r\n \t<li>Using directive ( importing all elements)<\/li>\r\n \t<li>Using declaration (importing single element at a time)<\/li>\r\n \t<li>With keyword \u2018using\u2019, names are imported in current scope where used<\/li>\r\n \t<li>Namespaces are open \u2013 they can be in several independent header files<\/li>\r\n \t<li>Namespace can be given alias name<\/li>\r\n \t<li>Namespace can be anonymous, i.e. without name<\/li>\r\n \t<li>Namespace can be extended to have additional elements<\/li>\r\n \t<li style=\"text-align: justify\">Namespace can be non-contiguous. Its definition can be continued and extended in same file or over multiple files, it is not redefined or overridden<\/li>\r\n \t<li>Use of namespace do not incur any runtime overhead, but it may decrease readability<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n<strong>References<\/strong>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n1) Stanley Lippmann, \u201cC++ Primer\u201d, Pearson Education.\r\n\r\n2) Bjarne Stroustrup, \u201cThe C++ Programming Language\u201d, Pearson Education.\r\n\r\n3) Scott Mayer, \u201cEffective C++\u201d, Addison Wesley.\r\n\r\n4) Bhushan Trivedi , Programming with ANSI C++, 2\/e , Oxford University Press.\r\n\r\n5) Yashavant P. Kanetkar \u201cLet Us C++\u201d , Bpb Publications.\r\n\r\n6) Abhiram G. Ranade, \u201cAn Introduction to Programming through C++\u201d , McGraw Hill\r\n\r\n7) Ellis and B. Stroustrup \u201cAnnotated C++ Reference Manual\u201d, http:\/\/www.stroustrup.com\/arm.html\r\n\r\n8) Herbert Schildt, \u201cComplete Reference C++\u201d, McGraw Hill Publications.\r\n\r\n9) Ashok Kamthane, \u201cObject Oriented Programming with ANSI and Turbo C++\u201d, Pearson Education\r\n\r\n10) E Balaguruswami, \u201cObject Oriented Programming With C++\u201d, Tata McGraw Hill\r\n\r\n11) \u201cC++ FAQs\u201d, Pearson Education.\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;","rendered":"<p><strong>Introduction<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In C++, identifiers may have either local or global scope. C++ allows unique names in specific scope. When C++ application uses libraries from different vendors or when application is developed using multiple source files written by various programmers, there are chances of having identifiers with same name in global scope namespace. This introduces a problem of name conflict. Namespace helps to prevent such name conflict by allowing identifiers to be grouped in a user-defined namespace.<\/p>\n<p>&nbsp;<\/p>\n<p>What is namespace?<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Namespace is a declarative region providing a scope to the identifiers contained in it. The identifiers are named entities, such as variable, function, class, object, typename, struct, enum or other compound types. Identifiers should have unique names in a namespace.<\/p>\n<p>&nbsp;<\/p>\n<p>Why namespace?<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Before understanding the need of namespace, let us recall the knowledge about global and local scope of variables. Namespace is sometimes referred to as scope also.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Named entities, such as variables, functions, class need to be declared before being used in C++. Depending upon the location of their declaration in a program, entities are having their scope or visibility.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.1. Local namespace<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">An entity declared within a block has <em>block scope<\/em>. Block may be introduced using statements in a pair of curly braces. Some examples of blocks are functions, loops, conditions. Parameters of a function and entities declared within a block are local to the block. Such entities are visible only within the specific block in which they are declared, but not outside it. Their storage class is auto; i.e. they are created when the block is executed and their life is till the end of block. Variables with block scope are known as <em>local variables<\/em>. They are not initialized with any default value.<\/p>\n<p>&nbsp;<\/p>\n<p>Local variables are in local namespace.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.2. Global namespace<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">An entity declared outside any block has <em>global scope<\/em>, meaning that its name is valid anywhere in the code. Storage for global entities is static. Their life is for the entire duration of the program. Variables with global scope are known as global variables. By default, variables with <em>static storage<\/em> are initialized to zeroes, so global variables have zero as their initial value.<\/p>\n<p>&nbsp;<\/p>\n<p>Global variables are in global namespace.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.3. Unique name in namespace<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>In each scope, may be block (local) or global, a name can represent only one entity. Thus, there cannot be two variables with the same name in the same scope.<\/p>\n<p>&nbsp;<\/p>\n<p>int function1 ()<\/p>\n<p>{<\/p>\n<p>int x;<\/p>\n<p>&nbsp;<\/p>\n<p>x = 0;<\/p>\n<p>&nbsp;<\/p>\n<p>double x; \/\/ wrong: name already used in this scope x = 0.0;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Blocks may be nested. The visibility of an entity with <em>block scope<\/em> extends until the end of the block, including inner blocks. An inner block, being a different block, can re-utilize a name existing in an outer block scope to refer to a different entity. In such case, the name will refer to a different entity within the inner block, hiding the entity with the same name in outer block scope.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the following example, note that variable y of outer block has a scope in nested block also. There is no variable named y in inner block. But variable named x occurs in outer block as well as in inner block. Re-utilizing variable name x in inner block hides variable x of outer block. See global variable named x declared outside block. To refer it within block, it requires using scope resolution operator.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-216 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-83.png\" alt=\"\" width=\"787\" height=\"379\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-83.png 787w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-83-300x144.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-83-768x370.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-83-65x31.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-83-225x108.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-83-350x169.png 350w\" sizes=\"auto, (max-width: 787px) 100vw, 787px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\">cout &lt;&lt; &#8220;x: &#8221; &lt;&lt; x &lt;&lt; &#8216;\\n&#8217;;<\/p>\n<p style=\"text-align: center\">cout &lt;&lt; &#8220;y: &#8221; &lt;&lt; y &lt;&lt; &#8216;\\n&#8217;;<\/p>\n<p style=\"text-align: center\">}<\/p>\n<p style=\"text-align: center\">cout &lt;&lt; &#8220;outer block:\\n&#8221;;<\/p>\n<p style=\"text-align: center\">cout &lt;&lt; &#8220;x: &#8221; &lt;&lt; x &lt;&lt; &#8216;\\n&#8217;;<\/p>\n<p style=\"text-align: center\">cout &lt;&lt; &#8220;y: &#8221; &lt;&lt; y &lt;&lt; &#8216;\\n&#8217;;<\/p>\n<p style=\"text-align: center\">cout &lt;&lt; &#8220;global variable x: &#8221; &lt;&lt; ::x &lt;&lt; &#8216;\\n&#8217;;<\/p>\n<p style=\"text-align: center\">return 0;<\/p>\n<p style=\"text-align: center\">}<\/p>\n<p><strong>1.4. Need of namespace<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>As explained, only one entity can exist with a particular name in a particular scope or namespace in C++.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">For local variables in block scope, there is hardly a chance for entities with same name; because blocks tend to be relatively short and names have particular purposes within them. But, for global scope, chances are very high for having multiple entities with same name as explained in following scenario.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Real applications are usually very large and divided into multiple modules or tasks. These modules are mostly developed by more than one developer in an independent manner. It is possible that different developers used same variable name or class name for different purposes in their programs. To produce the final application, these separate source files are compiled and linked. Application will not be created successfully due to conflict in names. Error will occur considering re-declaration of a variable.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">For example, consider an event management application consisting of many program files. Assume one file having variable \u2018length\u2019 referring to number of days of an event. In another file, variable \u2018length\u2019 is used to store number of characters used in the string representing event name. While linking these two files, problem will arise due to name clash in variable \u2018length\u2019.<\/p>\n<p>&nbsp;<\/p>\n<p>Many times, application may include libraries developed by different vendors. Hence, it is possible to have entities with same name in different libraries.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">For example, assume library1 containing class Angle in degree and library2 containing class Angle in radian. When these two libraries are included in a program, occurrence of repeated entity name Angle will create a conflict.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">C++ do not allow two entities with the same name in the same scope. Entities declared in separate files or libraries share global namespace. Hence, multiple definitions of names or name clashes may be encountered while linking the separate modules.<\/p>\n<p>&nbsp;<\/p>\n<p>To solve this problem, some workarounds may be like<\/p>\n<ul>\n<li>vendors or developers should cooperate with some strategy to have unique names<\/li>\n<li>use dummy classes or structs to group names<\/li>\n<\/ul>\n<p>C++ provides a better solution with namespace mechanism to overcome the problem of name clashes in the global scope.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The namespace mechanism allows global scope to be partitioned into number of sub-scopes, each sub-scope having its own name. In essence, a namespace defines a user-defined scope. User can create namespace to hold logical grouping of unique identifiers. An identifier defined in a namespace is associated only with that namespace. Thus, identifiers of one namespace do not conflict with identifiers in another user-defined namespace or global namespace.<\/p>\n<p>&nbsp;<\/p>\n<p>Thus, namespace provides a method for preventing name conflicts in large projects.<\/p>\n<p>&nbsp;<\/p>\n<p>Using namespace, one can define the context in which names are defined.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Namespaces <\/strong>are used to organize code into logical groups and prevent name collisions that can occur especially when the code base includes multiple libraries.<\/p>\n<p>&nbsp;<\/p>\n<p>Using namespace\u00a0 \u00a0Using namespace involves:<\/p>\n<ul>\n<li>Creating namespace with unique identifiers as its members or elements<\/li>\n<li>Accessing elements or members of namespace<\/li>\n<\/ul>\n<p><strong>1.5. Creating Namespace<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The syntax to declare a namespaces is: namespace namespace_name<\/p>\n<p>&nbsp;<\/p>\n<p>{ \/\/namespace body<\/p>\n<ul>\n<li>\/\/ identifier declarations<\/li>\n<\/ul>\n<p>}<\/p>\n<ul>\n<li>A namespace definition begins with the keyword <strong>namespace<\/strong> followed by the user-defined name of namespace and its body in a pair of curly brackets.<\/li>\n<li>Body part contains identifiers as its members having unique name. Identifiers may be variables, constants, functions, class, objects of class etc.<\/li>\n<li>Namespace definition doesn&#8217;t terminate with a semicolon.<\/li>\n<li>Function or class can be declared in namespace and defined outside namespace.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Note that a namespace definition must appear either at file scope, or immediately within another namespace definition. It cannot be defined at block scope, so cannot be defined in main(). Following example illustrates declaring variable, function, class and object in a namespace named myNamespace.<\/p>\n<p>&nbsp;<\/p>\n<p>namespace myNamespace<\/p>\n<p>{<\/p>\n<p>int num = 5; \/\/ variable declaration<\/p>\n<p>&nbsp;<\/p>\n<p>int isLeap(int y) \/\/ non-member function declaration<\/p>\n<p>{<\/p>\n<p>if ( (y%400 ==0) || (y%4==0 &amp;&amp; y%100 !=0) ) return 1; else return 0;<\/p>\n<p>}<\/p>\n<p>class Point\u00a0\u00a0\u00a0\u00a0 \/\/ class declaration<\/p>\n<p>{<\/p>\n<p>float x,y;<\/p>\n<p>public:<\/p>\n<p>Point() {x = y = 0;}<\/p>\n<p>Point(float p1, float p2) {x=p1; y=p2;}<\/p>\n<p>void show() {cout &lt;&lt; &#8216;(&#8216; &lt;&lt; x &lt;&lt; &#8216;,&#8217; &lt;&lt; y &lt;&lt; &#8216;)&#8217;;} }; \/\/ end class<\/p>\n<p>Point pt; \/\/ object declaration<\/p>\n<p>&nbsp;<\/p>\n<p>} \/\/ end myNamespace<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Function and class can be declared inside the namespace and defined outside it <\/strong>using scope resolution operator as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>namespace myNamespace<\/p>\n<p>{<\/p>\n<p>int num = 5; \/\/ variable declaration<\/p>\n<p>int isLeap(int y); \/\/ non-member function declaration<\/p>\n<p>class Point;\u00a0\u00a0\u00a0\u00a0 \/\/ class declaration<\/p>\n<p>\/\/Point pt; \/\/ object declaration, error:&#8217;pt&#8217; uses undefined class<\/p>\n<p>&#8216;myNamespace::Point&#8217;<\/p>\n<p>} \/\/ end myNamespace<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/defining function and class outside namespace using scope resolution operator int myNamespace::isLeap(int y) \/\/ non-member function definition<\/p>\n<p>{<\/p>\n<p>if ( (y%400 ==0) || (y%4==0 &amp;&amp; y%100 !=0) ) return 1; else return 0;<\/p>\n<p>}<\/p>\n<p>class myNamespace::Point\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ class definition outside namespace<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>float x,y;<\/p>\n<p>public:<\/p>\n<p>Point() {x = y = 0;}<\/p>\n<p>&nbsp;<\/p>\n<p>Point(float p1, float p2) {x=p1; y=p2;}<\/p>\n<p>&nbsp;<\/p>\n<p>void show() {cout &lt;&lt; &#8216;(&#8216; &lt;&lt; x &lt;&lt; &#8216;,&#8217; &lt;&lt; y &lt;&lt; &#8216;)&#8217;;} }; \/\/ end class<\/p>\n<p>&nbsp;<\/p>\n<p>It is possible to define <strong>class in namespace with its methods defined outside<\/strong> <strong>namespace <\/strong>as follows:<\/p>\n<p>namespace myNamespace<\/p>\n<p>{<\/p>\n<p>int num = 5; \/\/ variable declaration<\/p>\n<p>int isLeap(int y); \/\/ non-member function declaration class Point \/\/ class definition<\/p>\n<p>{<\/p>\n<p>float x,y;<\/p>\n<p>public:<\/p>\n<p>Point() {x = y = 0;}<\/p>\n<p>Point(float p1, float p2) {x=p1; y=p2;}<\/p>\n<p>void show(); \/\/ prototype declaration<\/p>\n<p>}; \/\/ end class<\/p>\n<p>&nbsp;<\/p>\n<p>} \/\/ end myNamespace<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ function defined outside namespace<\/p>\n<p>int myNamespace::isLeap(int y) \/\/ non-member function definition<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>if ( (y%400 ==0) || (y%4==0 &amp;&amp; y%100 !=0) ) return 1; else return 0;<\/p>\n<p>}<\/p>\n<ul>\n<li>\/\/ class method show() defined outside class, outside namespace void myNamespace::Point::show() \/\/ definition<\/li>\n<\/ul>\n<p>{cout &lt;&lt; &#8216;(&#8216; &lt;&lt; x &lt;&lt; &#8216;,&#8217; &lt;&lt; y &lt;&lt; &#8216;)&#8217;;}<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.6. Accessing elements of namespace<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">All identifiers within the scope are directly accessible without qualification. But outside the namespace scope, identifiers can be accessed by using the fully qualified name, or by using <em>Declaration<\/em> for a single identifier, or by using <em>Directive<\/em> for all the identifiers in the namespace. Thus there are three ways that code outside namespace can access their elements.<\/p>\n<ul>\n<li>Using fully qualified name with scope resolution operator ::<\/li>\n<li>Importing entire namespace using directive<\/li>\n<li>Importing single element at a time from namespace using declaration<\/li>\n<\/ul>\n<p><strong>1.6.1. Accessing namespace elements using scope resolution operator<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Any name (identifier) declared in a namespace can be explicitly referred using the namespace&#8217;s name and the scope resolution operator with the identifier. This type of accessing name is also referred to as fully qualified name. Following example defines identifier named \u2018num\u2019 in global namespace and in user-defined namespace. Identifier of user-defined namespace is referred using fully qualified name.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;iostream&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>int num=50; \/\/ global var<\/p>\n<p>namespace myNamespace<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>int num = 5; \/\/ variable declaration<\/p>\n<p>int isLeap(int y) \/\/ non-member function definition<\/p>\n<p>{<\/p>\n<p>if ( (y%400 ==0) || (y%4==0 &amp;&amp; y%100 !=0) ) return 1; else return 0;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>class Point\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ class definition<\/p>\n<p>{<\/p>\n<p>float x,y;<\/p>\n<p>&nbsp;<\/p>\n<p>public:<\/p>\n<p>Point() {x = y = 0;}<\/p>\n<p>Point(float p1, float p2) {x=p1; y=p2;}<\/p>\n<p>&nbsp;<\/p>\n<p>void show() {cout &lt;&lt; &#8216;(&#8216; &lt;&lt; x &lt;&lt; &#8216;,&#8217; &lt;&lt; y &lt;&lt; &#8216;)&#8217;;} }; \/\/ end class<\/p>\n<p>Point pt; \/\/ object declaration<\/p>\n<p>} \/\/ end myNamespace<\/p>\n<p>&nbsp;<\/p>\n<p>int main()<\/p>\n<p>{<\/p>\n<p>cout &lt;&lt; &#8220;Global scope num: &#8221; &lt;&lt; num &lt;&lt; endl;<\/p>\n<p>cout &lt;&lt; &#8220;myNamespace scope num: &#8221; &lt;&lt; myNamespace::num &lt;&lt; endl;<\/p>\n<p>cout &lt;&lt; &#8220;Enter year:&#8221;;<\/p>\n<p>cin &gt;&gt; num;<\/p>\n<p>if (myNamespace::isLeap(num)) cout &lt;&lt; num &lt;&lt; &#8221; is a leap year&#8221; &lt;&lt; endl;<\/p>\n<p>else cout &lt;&lt; num &lt;&lt; &#8221; is not a leap year&#8221; &lt;&lt; endl;<\/p>\n<p>myNamespace::pt.show();<\/p>\n<p>myNamespace::Point pt1(3.5, 4.5);<\/p>\n<p>pt1.show();<\/p>\n<p>return 0;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Global scope num: 50<\/p>\n<p>myNamespace scope num: 5<\/p>\n<p>Enter year:1900<\/p>\n<p>1900 is not a leap year<\/p>\n<p>&nbsp;<\/p>\n<p>(0,0)(3.5,4.5)<\/p>\n<p>&nbsp;<\/p>\n<p>Refer following example having three variables with same name; each in global, local and user-defined namespace scope.<\/p>\n<ul>\n<li>\/\/ namescope element access with qualified name<\/li>\n<li>\/\/ 3 variables with same name but in different scopes #include &lt;iostream&gt;<\/li>\n<\/ul>\n<p>using namespace std;<\/p>\n<p>&nbsp;<\/p>\n<p>int num=50; \/\/ global variable<\/p>\n<p>namespace myFirstNamespace<\/p>\n<p>&nbsp;<\/p>\n<p>{\u00a0 int num = 5; }<\/p>\n<p>&nbsp;<\/p>\n<p>int main()<\/p>\n<p>{<\/p>\n<p>int num = 70; \/\/ block scope (local variable) cout &lt;&lt; &#8220;Global scope num: &#8221; &lt;&lt; ::num &lt;&lt; endl; cout &lt;&lt; &#8220;Block scope num: &#8221; &lt;&lt; num &lt;&lt; endl;<\/p>\n<p>cout &lt;&lt; &#8220;myFirstNamespace scope num: &#8221; &lt;&lt; myFirstNamespace::num &lt;&lt; endl; return 0;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>Global scope num: 50<\/p>\n<p>Block scope num: 70<\/p>\n<p>myFirstNamespace scope num: 5<\/p>\n<p>Identifiers will be looked up first in local namespace. So, global declarations with the same name can be accessed using scope resolution (::) .<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.6.2. Importing namespace elements with keyword \u2018using\u2019<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The <strong>using<\/strong> keyword can be used to import entire or part of namespace into current declarative region.<\/p>\n<p style=\"text-align: justify\">Names introduced into a namespace scope with \u2018using\u2019 can be used with or without qualification from other scopes.<\/p>\n<p style=\"text-align: justify\">When we use <strong>using directive<\/strong>, we import all the names in the namespace. But with <strong>using declaration<\/strong>, we import one specific name at a time.<\/p>\n<p style=\"text-align: justify\">Namespace can be introduced to another named namespace or to a local block scope or file scope in a program depending upon where \u2018using\u2019 is introduced.<\/p>\n<p style=\"text-align: justify\">Names introduced with \u2018using\u2019 keyword obey normal scope rules. The name is visible from the point of the \u2018using\u2019 to the end of the scope in which the directive\/declaration is found.<\/p>\n<p style=\"text-align: justify\">Entities with the same name defined in an outer scope are hidden.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.7. Examples to understand use of directive and declaration at various scopes<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Syntax for importing entire namespace with \u2018using directive\u2019:<\/p>\n<p>using namespace &lt;namespace_name&gt; Syntax for importing single element from namespace with \u2018using declaration\u2019:<\/p>\n<p>using &lt;namespace_name::element_name&gt;<\/p>\n<p>Note that keyword \u2018namespace\u2019 is not used in \u2018using declaration\u2019.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.7.1. Using Directive at block (local) scope<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Following example shows importing all names from namespace using directive at block scope. Within block, namespace identifiers are referred without qualification.<\/p>\n<ul>\n<li>\/\/ importing entire namespace using directive<\/li>\n<li>\/\/ importing namespace at block scope #include &lt;iostream&gt;<\/li>\n<\/ul>\n<p>using namespace std; namespace first<\/p>\n<p>&nbsp;<\/p>\n<p>{ int x = 5; int y = 10;} namespace second<\/p>\n<p>{ double x = 3.1416; int y = 20; }<\/p>\n<p>int main()<\/p>\n<p>{<\/p>\n<p>{ \/\/ block1<\/p>\n<p>using namespace first; \/\/ import all identifiers of first in current block\u2019s scope cout &lt;&lt; x &lt;&lt; &#8216;, &#8216; &lt;&lt; y &lt;&lt; &#8216;\\n&#8217;;<\/p>\n<p>}<\/p>\n<p>{\/\/ block2<\/p>\n<p>using namespace second; \/\/ import all identifiers of second in current block\u2019s scope<\/p>\n<p>cout &lt;&lt; x &lt;&lt; &#8216;, &#8216; &lt;&lt; y &lt;&lt; &#8216;\\n&#8217;;<\/p>\n<p>}<\/p>\n<p>return 0;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>5, 10<\/p>\n<p>3.1416, 20<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Removing blocks from \u2018main\u2019 in above code will raise ambiguity in names x and y when elements of namespace second are accessed. Note that variables x and y are not redefined here, they are just imported to be in current scope. So, error is not flagged at the time of importing with using directive.<\/p>\n<p>&nbsp;<\/p>\n<p>int main()<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>using namespace first; \/\/ import all identifiers of first in current block\u2019s scope cout &lt;&lt; x &lt;&lt; &#8216;,&#8217; &lt;&lt; y &lt;&lt; &#8216;\\n&#8217;;<\/p>\n<p>using namespace second; \/\/ import all identifiers of second in current block\u2019s scope<\/p>\n<p>cout &lt;&lt; x &lt;&lt; &#8216;,&#8217; &lt;&lt; y &lt;&lt; &#8216;\\n&#8217;; \/\/ ambiguous x and y<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To get rid of error due to ambiguity in names, use fully qualified name like second::x and second::y. Note that it is always safer to use fully qualified name.<\/p>\n<p style=\"text-align: justify\">Note that identifiers of local namespace hide identical names of namespace even when directive is used. Try defining int x = 555 in main() and see the result.<\/p>\n<p><strong>1.7.2. Using Directive at file scope (global within single source file)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">When \u2018using namespace\u2019 or \u2018using\u2019 is used outside any block like global variables, its elements have file scope. It means that such identifiers are visible only in this specific file; they are not available in other files. This is how it differs from normal global scope. Variables defined in global namespace can be availed in other files by declaring them as \u2018extern\u2019. Identifiers of namespace cannot be declared as \u2018extern\u2019. If needed in another file, they need to be imported with \u2018using\u2019 keyword.<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>\/\/ importing entire namespace using directive<\/li>\n<li>\/\/ importing namespace at file (global in file) scope #include &lt;iostream&gt;<\/li>\n<\/ul>\n<p>using namespace std;<\/p>\n<p>&nbsp;<\/p>\n<p>namespace first<\/p>\n<p>&nbsp;<\/p>\n<p>{ int x = 5; int y = 10;} namespace second<\/p>\n<p>{ double x = 3.1416; int y = 20; }<\/p>\n<p>&nbsp;<\/p>\n<p>using namespace first; \/\/ import all identifiers of namespace first here, at file scope<\/p>\n<p>&nbsp;<\/p>\n<p>int main ()<\/p>\n<p>{<\/p>\n<p>cout &lt;&lt; &#8220;from first namespace: &#8221; &lt;&lt; x &lt;&lt; &#8220;, &#8221; &lt;&lt; y &lt;&lt; &#8216;\\n&#8217;;<\/p>\n<p>&nbsp;<\/p>\n<p>cout &lt;&lt; &#8220;from second namespace: &#8221; &lt;&lt; second::x &lt;&lt; &#8220;, &#8221; &lt;&lt; second::y &lt;&lt; &#8216;\\n&#8217;; return 0;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>from first namespace: 5, 10<\/p>\n<p>from second namespace: 3.1416, 20<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">When identifiers in global namespace collide with identifiers in namespace scope, it results in compile time error. Adding int x; before main() in above code will give an error due to ambiguity at the point where x is used. Error is not flagged at the point of defining x with the same name or at the time of importing namespace.In this example, what will happen if both namespaces are imported at global or file scope here? Will it create name conflict problem?<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.7.3. Using Directive at another namespace scope<\/strong><\/p>\n<ul>\n<li>\/\/ importing entire namespace with using directive into another namespace #include &lt;iostream&gt;<\/li>\n<\/ul>\n<p>using namespace std;<\/p>\n<p>namespace second<\/p>\n<p>{ double x = 3.1416; int y = 20; } namespace third<\/p>\n<p>{ using namespace second; \/\/ import namespace second in namespace third double xx = 7.14; int yy = 50; }<\/p>\n<p>using namespace third; \/\/ import namespace third here, global scope<\/p>\n<p>int main ()<\/p>\n<p>{<\/p>\n<p>cout &lt;&lt; &#8220;from third namespace: &#8221; &lt;&lt; x &lt;&lt; &#8220;, &#8221; &lt;&lt; y<\/p>\n<ul>\n<li>&lt;&lt; &#8220;, &#8221; &lt;&lt; xx &lt;&lt; &#8220;, &#8221; &lt;&lt; yy &lt;&lt; &#8216;\\n&#8217;; return 0;<\/li>\n<\/ul>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>from third namespace: 3.1416, 20, 7.14, 50<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.7.4. Using declaration at various scopes<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2018Using declaration\u2019 enables to import only one element of namespace at a time in current scope where it is used. Refer following example where a single element y of namespace first is imported at global scope. As seen in previous examples, it can be imported at block scope or at another namespace scope also.<\/p>\n<ul>\n<li>\/\/ importing single element at a time from namespace using declaration #include &lt;iostream&gt;<\/li>\n<\/ul>\n<p>using namespace std;<\/p>\n<p>&nbsp;<\/p>\n<p>namespace first<\/p>\n<p>{ int x = 5; int y = 10;}<\/p>\n<p>using first::y; \/\/ using declarative at global scope<\/p>\n<p>int x; \/\/ global variable<\/p>\n<p>&nbsp;<\/p>\n<p>int main ()<\/p>\n<p>{<\/p>\n<p>int x=50; \/\/ local variable<\/p>\n<ul>\n<li>\/\/ global ::x; local x; first::x and y from namespace first cout &lt;&lt; ::x &lt;&lt; &#8220;, &#8221; &lt;&lt; x &lt;&lt; &#8220;, &#8221; &lt;&lt; first::x &lt;&lt; &#8220;, &#8221; &lt;&lt; y &lt;&lt; &#8216;\\n&#8217;; return 0;<\/li>\n<\/ul>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Output:<\/p>\n<p>&nbsp;<\/p>\n<p>0, 50, 5, 10<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In above example, importing first::x at global scope or block scope of main() will raise conflict as there is variable x with same name defined in the same scope. At global scope, it conflicts with global x; and at local scope, it conflicts with local x. In such cases, one may use \u2018using declaration\u2019 to import single element of namespace instead of importing all elements of namespace. Here, only element y of namespace first is imported in global scope.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.7.5. Overriding name when same name is imported from different namespaces in same scope<\/strong><\/p>\n<ul>\n<li style=\"text-align: justify\">\/\/ importing entire namespace using directive and single element using declaration<\/li>\n<li style=\"text-align: justify\">\/\/ overriding name when same name is imported from namespace scope<\/li>\n<\/ul>\n<p>#include &lt;iostream&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>using namespace std;<\/p>\n<p>namespace first { int x = 5; int y = 10;}<\/p>\n<p>namespace second { double x = 3.1416; int y = 20; }<\/p>\n<p>int main ()<\/p>\n<p>{<\/p>\n<p>using namespace first; \/\/ x, y of first in block scope of main()<\/p>\n<p>using second::x; \/\/ identifier x of namespace second overrides x of namespace first cout &lt;&lt; x &lt;&lt; &#8220;, &#8221; &lt;&lt; y &lt;&lt; &#8216;\\n&#8217;; \/\/ refers to second::x, first::y<\/p>\n<p>cout &lt;&lt; first::x &lt;&lt; &#8216;\\n&#8217;; \/\/ x of first accessible using fully qualified name return 0;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>3.1416, 10<\/p>\n<p>5<\/p>\n<p style=\"text-align: justify\">Here, variable x of second namespace is imported after variables x and y of first namespace at block scope. Name imported later overrides already imported names. As seen in all above examples, <strong>using<\/strong> and <strong>using namespace<\/strong> have validity only in the same block in which they are stated or in the entire source code file if they are used directly in the global scope.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Namespace std<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>All the entities (variables, types, constants, and functions) of the standard C++ library are declared within the std namespace. Most examples used here include following line at file scope:<\/p>\n<p>&nbsp;<\/p>\n<p>using namespace std;<\/p>\n<p>&nbsp;<\/p>\n<p>This introduces direct visibility of all the names of the std namespace into the code. So, objects like cout and cin defined in std namespace are accessible without scope resolution operator.\u00a0 Without use of \u2018using namespace std\u2019, one will have to use std::cout and std::cin.<\/p>\n<p>&nbsp;<\/p>\n<p>For example, instead of<\/p>\n<p>cout &lt;&lt; \u201cHello world\u201d;<\/p>\n<p>it requires writing<\/p>\n<p>std::cout &lt;&lt; \u201cHello world\u201d;<\/p>\n<p>&nbsp;<\/p>\n<p>Including line \u2018using namespace std;\u2019 at file scope facilitate comprehension and shorten the length of the code. Many programmers prefer to qualify each of the elements of the standard library used in their programs.<\/p>\n<p>&nbsp;<\/p>\n<p>Whether the elements in the std namespace are introduced with using declarations or are fully qualified on every use, it does not change the behaviour or efficiency of the resulting program in any way. It is mostly a matter of style preference. Although for projects with mixing libraries, explicit qualification tends to be preferred.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Anonymous Namespace<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">It is possible to create a namespace without giving any name. Such namespaces are called anonymous or unnamed namespaces.\u00a0Unnamed namespace can contain everything like named namespace. But, as it is not given any name, its elements cannot be accessed using qualified name or cannot be imported with \u2018using\u2019 keyword. Elements of unnamed namespace are available at file scope and can be accessed just like normal global variables within the file only.<\/p>\n<p>Refer following example.<\/p>\n<p>\/\/ Unnamed namespace<\/p>\n<p>#include &lt;iostream&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>namespace \/\/ anonymous, not given any name<\/p>\n<p>{ int x; int y; }<\/p>\n<p>int main ()<\/p>\n<p>{<\/p>\n<p>cout &lt;&lt; &#8220;Enter two integres:&#8221;;<\/p>\n<p>cin &gt;&gt; x &gt;&gt; y;<\/p>\n<p>cout &lt;&lt; x &lt;&lt; &#8220;, &#8221; &lt;&lt; y &lt;&lt; &#8216;\\n&#8217;;<\/p>\n<p>return 0;<\/p>\n<p>}<\/p>\n<p>Output:<\/p>\n<p>Enter two integres:5 10<\/p>\n<p>5, 10<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Even though variables x and y of anonymous namespace are accessed like normal global variables, they are not available in other files. They cannot be declared as \u2018extern\u2019 in other files.Unnamed namespace is unique within a file, i.e. there cannot be more than one unnamed namespace in a file. When unnamed space is defined again, it is considered as an extension to an existing unnamed space.<\/p>\n<ul>\n<li>\/\/ redefining unnamed space extends existing unnamed space #include &lt;iostream&gt;<\/li>\n<\/ul>\n<p>using namespace std;<\/p>\n<p>&nbsp;<\/p>\n<p>namespace \/\/ anonymous, not given any name<\/p>\n<p>{ int x; int y; }<\/p>\n<p>namespace \/\/ anonymous, extended<\/p>\n<p>{ int xx=10; int yy=20; } int main ()<\/p>\n<p>{<\/p>\n<p>cout &lt;&lt; &#8220;Enter two integres:&#8221;; cin &gt;&gt; x &gt;&gt; y;<\/p>\n<p>cout &lt;&lt; x &lt;&lt; &#8220;, &#8221; &lt;&lt; y &lt;&lt; &#8216;\\n&#8217;; cout &lt;&lt; xx &lt;&lt; &#8220;, &#8221; &lt;&lt; yy &lt;&lt; &#8216;\\n&#8217;;<\/p>\n<p>return 0;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>Enter two integres:50 70<\/p>\n<p>&nbsp;<\/p>\n<p>50, 70<\/p>\n<p>10, 20<\/p>\n<p>What if a variable with the same name occurs at local and global scope? Refer following example.<\/p>\n<ul>\n<li>\/\/ variables with same name in global, local and anonymous namespace scope #include &lt;iostream&gt;<\/li>\n<\/ul>\n<p>using namespace std;<\/p>\n<p>&nbsp;<\/p>\n<p>namespace \/\/ anonymous, not given any name<\/p>\n<p>{ int x=5; int y=25; }<\/p>\n<p>namespace \/\/ anonymous, not given any name<\/p>\n<p>{ int xx=10; int yy=20; } int x=55; \/\/global<\/p>\n<p>int main ()<\/p>\n<p>{<\/p>\n<p>int x=75; \/\/ local<\/p>\n<p>cout &lt;&lt; ::x &lt;&lt; &#8220;, &#8221; &lt;&lt; x &lt;&lt; &#8220;, &#8221; &lt;&lt; y &lt;&lt; &#8216;\\n&#8217;;<\/p>\n<p>cout &lt;&lt; xx &lt;&lt; &#8220;, &#8221; &lt;&lt; yy &lt;&lt; &#8216;\\n&#8217;;<\/p>\n<p>return 0;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>55, 75, 25<\/p>\n<p>&nbsp;<\/p>\n<p>10, 20<\/p>\n<p>&nbsp;<\/p>\n<p>It can be observed that a variable in global scope is still available using scope resolution operator, but local variable hides the same name from anonymous namespace.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Nested Namespace<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>A namespace definition can be nested within another namespace definition. Every namespace definition must appear either at file scope or immediately within another namespace definition.\u00a0\u00a0Namespace can be nested up to any level.Elements of nested namespace can be accessed using qualified name and can also be imported with \u2018using\u2019.<\/p>\n<p>&nbsp;<\/p>\n<p>See following example.<\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;iostream&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>namespace namespaceOuter<\/p>\n<p>{<\/p>\n<p>float num1 = 10.5;<\/p>\n<p>namespace namespaceInner1 \/\/nested<\/p>\n<p>{ float num1 = 25.25; float num2 = 30.75; }<\/p>\n<p>}<\/p>\n<p>int main ()<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>\/\/Accessing elements of nested namespace<\/p>\n<p>\/\/With qualified name using scope resolution operator cout &lt;&lt; &#8220;outer num1:&#8221; &lt;&lt; namespaceOuter::num1 &lt;&lt; endl;<\/p>\n<p>cout &lt;&lt; &#8220;Inner num1:&#8221; &lt;&lt; namespaceOuter::namespaceInner1::num1 &lt;&lt; endl; cout &lt;&lt; &#8220;Inner num2:&#8221; &lt;&lt; namespaceOuter::namespaceInner1::num2 &lt;&lt; endl;<\/p>\n<p>\/\/With \u2018using\u2019 keyword:<\/p>\n<p>using namespace namespaceOuter::namespaceInner1;<\/p>\n<p>cout &lt;&lt; &#8221; with using, inner num1 and num2&#8230;&#8221; &lt;&lt; num1 &lt;&lt; &#8221; &#8221; &lt;&lt; num2 &lt;&lt; endl;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Output:<\/p>\n<p>&nbsp;<\/p>\n<p>outer num1:10.5<\/p>\n<p>Inner num1:25.25<\/p>\n<p>Inner num2:30.75<\/p>\n<p>with using, inner num1 and num2&#8230;25.25 30.75<\/p>\n<p>Namespace Aliasing<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Namespace aliasing allow the programmer to define an alternate name for a namespace.It is commonly used as a convenient shortcut for long or deeply-nested namespaces.\u00a0Existing namespace can be aliased with new name, with the following syntax: namespace new_name = current_name; Aliasing helps to reduce the length of long fully qualified names: namespace ns1 = namespaceOuter::namespaceInner1; cout &lt;&lt; ns1::num1 &lt;&lt; ns1::num2;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Aliasing also helps to get rid of changes in program code in case name of namespace is changed. Here, the purpose is somewhat similar to using #define. For example, if name of namespace namespaceInner1 is changed to namespaceLevel1, merely change in alias name helps in getting rid of changes wherever its elements are referred in a program.<\/p>\n<p>&nbsp;<\/p>\n<p>namespace ns1 = namespaceOuter::namespaceLevel1; \/\/ change in namespace name<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">cout &lt;&lt; ns1::num1 &lt;&lt; ns1::num2; \/\/ unaffected by name change due to alias<\/p>\n<p>\/\/Namespace aliasing<\/p>\n<p>#include &lt;iostream&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>namespace namespaceOuter<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>float num1 = 10.5;<\/p>\n<p>namespace namespaceInner1 \/\/nested<\/p>\n<p>{ float num1 = 25.25; float num2 = 30.75; }<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>int main ()<\/p>\n<p>{<\/p>\n<p style=\"text-align: justify\">namespace ns1 = namespaceOuter::namespaceInner1; \/\/ creating alias \/\/ using alias ns1 &#8211; ns1::num1, ns1::num2<\/p>\n<p>cout &lt;&lt; &#8220;Using alias &#8211; inner num1 and num2: &#8221; &lt;&lt; ns1::num1 &lt;&lt; &#8221; &#8221; &lt;&lt;<\/p>\n<p>ns1::num2 &lt;&lt; endl;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Using alias &#8211; inner num1 and num2: 25.25 30.75<\/p>\n<p>&nbsp;<\/p>\n<p>Extending Namespace<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A namespace definition can be extended within the same file or over multiple files. This enables to split the namespace by declaring its elements in several parts as per need. So it can be referred to as discontinuous namespace also.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Extending namespace is useful when a large application is developed by more than one programmer. Programmers may define identifiers as per their individual need in the namespace and can be added in program code with other identifiers in the same namespace.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Syntax to extend namespace is same as creating namespace. Defining namespace for the first time creates new namespace. Redefining existing namespace simply extends it adding new elements.<\/p>\n<p>&nbsp;<\/p>\n<p>Note: The namespace is not overwritten or overridden. It is either created or extended.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Consider following examples:<\/strong><\/p>\n<ul>\n<li>\/\/ extending named namespace #include &lt;iostream&gt;<\/li>\n<\/ul>\n<p>using namespace std; namespace myNamespace { float num1 = 10.5; } namespace myNamespace \/\/ extended myNamespace { float num2 = 30.75; }<\/p>\n<p>&nbsp;<\/p>\n<p>using namespace myNamespace;<\/p>\n<p>int main ()<\/p>\n<p>{<\/p>\n<p>cout &lt;&lt; &#8220;myNamespace num1 and num2&#8230;&#8221; &lt;&lt; num1 &lt;&lt; &#8221; &#8221; &lt;&lt; num2 &lt;&lt; endl; return 0;<\/p>\n<p>}<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>myNamespace num1 and num2&#8230;10.5 30.75<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Consider following example of namespace extended after importing its elements. Here, namespace have two function members f(int) and f(char). The namespace is extended after the using-declaration of elements from existing namespace with member f(int). The namespace is extended but additional declarations f(char) with same name do not become visible through the using-declaration (in contrast with using-directive).<\/p>\n<ul>\n<li>\/\/ extending named namespace after using declaration #include &lt;iostream&gt;<\/li>\n<\/ul>\n<p>using namespace std;<\/p>\n<p>&nbsp;<\/p>\n<p>namespace ns<\/p>\n<p>&nbsp;<\/p>\n<p>{ void f(int x) {cout &lt;&lt; x &lt;&lt; endl; } } using ns::f; \/\/ f now refers to ns::f(int)<\/p>\n<p>&nbsp;<\/p>\n<p>namespace ns \/\/ namespace extension<\/p>\n<p>{ void f (char c) {cout &lt;&lt; c &lt;&lt; endl; } } \/\/ added f(char) in namespace ns<\/p>\n<p>&nbsp;<\/p>\n<p>int main ()<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>f(&#8216;a&#8217;); \/\/ calls f(int), even though f(char) exists as f(char) not in scope ns::f(&#8216;a&#8217;); \/\/ explicit qualification<\/p>\n<p>using ns::f; \/\/ again using, now f is a synonym for both ns::f(int) and ns::f(char)<\/p>\n<p>f(&#8216;a&#8217;); \/\/ calls f(char)<\/p>\n<p>f(65); \/\/ calls f(int)<\/p>\n<p>return 0;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>97<\/p>\n<p>a<\/p>\n<p>a<\/p>\n<p>65<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Observe following from the output:<\/p>\n<ul>\n<li>first call f(\u2018a\u2019) calls f(int) as f(char) is not visible here and char can be implicitly converted to int.<\/li>\n<li style=\"text-align: justify\">In next line, call ns::f(\u2018a\u2019) with explicit qualification called f(char). It means f(char) is there in namespace, but it is yet not imported into current scope.With declaration \u2018using ns::f\u2019, now<\/li>\n<li style=\"text-align: justify\">f(char) is also in scope. So next calls f(\u2018a\u2019) and f(65) calls f(char) and f(int) respectively.<\/li>\n<\/ul>\n<p style=\"text-align: justify\">Modify above code to replace line \u2018using ns::f\u2019 with line \u2018using namespace ns\u2019 before extending namespace. See the effect. Now the output shows that it calls f(char), f(char), f(char) and f(int) displaying a, a, a and 65.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The point is: If the namespace is extended after the using-declaration of elements from existing namespace, the namespace is extended but additional declarations do not become visible through the using-declaration (in contrast with using-directive).<\/p>\n<p>&nbsp;<\/p>\n<p>Do namespace introduce any overhead?<\/p>\n<p>&nbsp;<\/p>\n<p>Namespace does not introduce any overhead at runtime as namespaces are resolved at compile time. All fully qualified name and using directive or declarations are handled at compile time. Use of namespace does affect the readability of the program. So it should be used only when needed.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>Namespace is a feature added in C++ and not present in C<\/li>\n<li>A namespace is a declarative region that provides a scope to the identifiers (names of the types, function, variables etc) inside it<\/li>\n<li>C++ allows unique names in a scope. Namespace enables using same names in different context without name conflict<\/li>\n<li>The namespace definition must be done at global scope, or nested inside another namespace<\/li>\n<li>Its syntax is somewhat like class syntax, but<\/li>\n<li>Identifiers cannot have access specification (public, private, protected)<\/li>\n<li>Namespace definition doesn&#8217;t terminate with a semicolon<\/li>\n<li>One cannot create instance of namespace<\/li>\n<li>Elements can be accessed using<\/li>\n<li>Qualification name with scope resolution operator<\/li>\n<li>Using directive ( importing all elements)<\/li>\n<li>Using declaration (importing single element at a time)<\/li>\n<li>With keyword \u2018using\u2019, names are imported in current scope where used<\/li>\n<li>Namespaces are open \u2013 they can be in several independent header files<\/li>\n<li>Namespace can be given alias name<\/li>\n<li>Namespace can be anonymous, i.e. without name<\/li>\n<li>Namespace can be extended to have additional elements<\/li>\n<li style=\"text-align: justify\">Namespace can be non-contiguous. Its definition can be continued and extended in same file or over multiple files, it is not redefined or overridden<\/li>\n<li>Use of namespace do not incur any runtime overhead, but it may decrease readability<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>References<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>1) Stanley Lippmann, \u201cC++ Primer\u201d, Pearson Education.<\/p>\n<p>2) Bjarne Stroustrup, \u201cThe C++ Programming Language\u201d, Pearson Education.<\/p>\n<p>3) Scott Mayer, \u201cEffective C++\u201d, Addison Wesley.<\/p>\n<p>4) Bhushan Trivedi , Programming with ANSI C++, 2\/e , Oxford University Press.<\/p>\n<p>5) Yashavant P. Kanetkar \u201cLet Us C++\u201d , Bpb Publications.<\/p>\n<p>6) Abhiram G. Ranade, \u201cAn Introduction to Programming through C++\u201d , McGraw Hill<\/p>\n<p>7) Ellis and B. Stroustrup \u201cAnnotated C++ Reference Manual\u201d, http:\/\/www.stroustrup.com\/arm.html<\/p>\n<p>8) Herbert Schildt, \u201cComplete Reference C++\u201d, McGraw Hill Publications.<\/p>\n<p>9) Ashok Kamthane, \u201cObject Oriented Programming with ANSI and Turbo C++\u201d, Pearson Education<\/p>\n<p>10) E Balaguruswami, \u201cObject Oriented Programming With C++\u201d, Tata McGraw Hill<\/p>\n<p>11) \u201cC++ FAQs\u201d, Pearson Education.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"author":4,"menu_order":30,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-jyotika-doshi"],"pb_section_license":""},"chapter-type":[],"contributor":[60],"license":[],"class_list":["post-215","chapter","type-chapter","status-publish","hentry","contributor-dr-jyotika-doshi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":6,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/215\/revisions"}],"predecessor-version":[{"id":347,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/215\/revisions\/347"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/215\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/media?parent=215"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapter-type?post=215"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/contributor?post=215"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/license?post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}