{"id":84001,"date":"2021-09-10T09:00:20","date_gmt":"2021-09-10T03:30:20","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=84001"},"modified":"2021-09-10T09:00:20","modified_gmt":"2021-09-10T03:30:20","slug":"cpp-namespaces","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/cpp-namespaces\/","title":{"rendered":"C++ Namespaces with examples"},"content":{"rendered":"<p>The C++ programming language offers various useful and efficient features and functionalities to programmers. It also supports object-oriented programming concepts. In C++, Namespaces are one of the most useful concepts.<\/p>\n<h3>What are Namespaces in C++?<\/h3>\n<p>With the help of Namespaces in C++, it is easy to handle applications by organizing too many classes. It is more like a container for identifiers. It is mainly used to put the names of its members in a different space to prevent name collisions which will occur when your program includes multiple libraries.<\/p>\n<p>The global namespace is also known as the root namespace in C++. You have to use namespace_name::class_name to access the class of a namespace.<\/p>\n<h3>Defining a namespace in C++<\/h3>\n<p>In C++, you have to use the namespace keyword followed by the namespace as below:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">namespace mynamespace{\n\/\/ declarations\n}\nint main(){\n\/\/ statements\n}\n<\/pre>\n<h4>Rules to follow before defining a namespace<\/h4>\n<p>In C++, there are certain rules which you have to follow before defining a namespace.<\/p>\n<ul>\n<li>You should always define a namespace at global scope or nested inside another namespace.<\/li>\n<li>There is no need to put a semicolon at the end of the namespace definition.<\/li>\n<li>For ease of use, you can put an alias name for your namespace name like below:-\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">namespace TechVidvan{\nvoid tutorial();\nclass Do{\n\/\/ class definition\n};\n}\nnamespace Tech = TechVidvan;<\/pre>\n<\/li>\n<li>There are no access specifiers in namespace declarations.<\/li>\n<li>You can also split the namespace into several units.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\nnamespace first{\nvoid fun() {\ncout &lt;&lt; \"C++ Namespaces!\" &lt;&lt; endl;\n}\n}\nnamespace second{\nvoid fun() {\ncout &lt;&lt; \"Namespaces in C++!\" &lt;&lt; endl;\n}\n}\n \nint main () {\nfirst::fun();\nsecond::fun();\nreturn 0;\n}\n<\/pre>\n<\/li>\n<\/ul>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">C++ Namespaces!<br \/>\nNamespaces in C++!<\/div>\n<h3>Using Namespaces in C++<\/h3>\n<p>In three ways, you can use a namespace in C++:-<\/p>\n<h4>1. Using Scope Resolution Operator<\/h4>\n<p>Using the namespace name, you can explicitly specify any name declared in a namespace and the scope resolution \u2018::\u2019 operator with the identifier.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">namespace Tech\n{\nclass A\n{\nstatic int a;\npublic:\nvoid fun();\n};\nclass B;    \n}\nint Tech::A::a=23; \t \nclass Tech::B\n{\nint b;\npublic:\nint val()\n{\ncout &lt;&lt; b;\n}\nB();   \n}\nTech::B::B()   \n{\n  b=0;\n}\n<\/pre>\n<h4>2. Using the directive<\/h4>\n<p>You can import an entire namespace into your program with the help of using directive. Let\u2019s say we created a header file named Namespace1.h:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">namespace Tech\n{\nint x;\nclass PleaseCheck\n{\nint i;\n};\n}\n<\/pre>\n<p>Below, we included the above namespace header file in Namespace2.h file:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">include \"Namespace1.h\";\n\nnamespace Tech1\n{\n  using namespace Tech;\n  PleaseCheck obj;\n  int y;\n}\n<\/pre>\n<p>Above, we imported the namespace Tech into namespace Tech1 and the class PleaseCheck is now accessible in the namespace Tech1.<\/p>\n<p>Below, we created the following program in a different file named test.cpp:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include \"Namespace2.h\";\n\nvoid test()\n{\nusing Namespace Tech1;\nPleaseCheck obj2;\n}\n<\/pre>\n<p>You can use namespace wherever you want with the help of using directive.<\/p>\n<h4>3. With Using declaration<\/h4>\n<p>With the help of using declaration, you can import one specific name at a time and it is available only inside the current scope.<\/p>\n<p><strong>NOTE:-<\/strong> You have to be careful of using the using declaration because it can override the name imported with using directive.<\/p>\n<p>Let\u2019s say we created a file named Namespace.h:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">namespace Tech\n{\nvoid fun1()\n{\ncout &lt;&lt; \"Just testing Function1 of namespace Tech!\\n\";\n}\nvoid fun2()\n{\ncout &lt;&lt; \"Just testing Function2 of namespace Tech!\\n\";\n}\n}\n\nnamespace Tech1\n{\nvoid fun1()\n{\ncout &lt;&lt; \"Just testing Function1 of namespace Tech1!\\n\";\n}\nvoid fun2()\n{\ncout &lt;&lt; \"Just testing Function2 of namespace Tech1!\\n\";\n}\n}<\/pre>\n<p>Below, we created a program named test.cpp and included the above header file into it.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include \"Namespace.h\";\n\nvoid testing()\n{\nusing namespace Tech;  \nusing Tech1::fun1;\nfun1();    \nTech::fun1();\n}<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">Just testing Function1 of namespace Tech1!<br \/>\nJust testing Function1 of namespace Tech!<\/div>\n<h3>Nested Namespace in C++<\/h3>\n<p>You can also define a namespace inside another namespace in C++.<br \/>\n<strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">namespace namespace_1{\n  \/\/ statements\n   namespace namespace_2{\n   \t\/\/ statements\n   }\n}\n<\/pre>\n<h3>Discontinuous Namespace in C++<\/h3>\n<p>It is mainly used to define a new namespace or you can add new elements to an existing namespace through it.<br \/>\n<strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">namespace namespace_name{\n   \/\/ code\n}<\/pre>\n<h3>Need for namespaces in C++<\/h3>\n<p>Let\u2019s say that you are in a situation where you have a function named abc() in your program and there is a predefined library available that has the same function abc(). Now, when we compile the program then the compiler will get confused between these two different abc() functions.<\/p>\n<p>So, to prevent this, the namespace is used as additional information to differentiate similar functions, variables, classes, etc. with the same name available in different libraries. It is used widely in several programs.<\/p>\n<h3>Summary<\/h3>\n<p>In this tutorial, we discussed namespaces in C++and how to define them. We talked over some rules which you should follow before working with the namespaces. Then we saw three ways in which you can use namespaces in C++. We talked over the discontinuous and nested namespace in C++. We discussed the need for namespaces in C++.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C++ programming language offers various useful and efficient features and functionalities to programmers. It also supports object-oriented programming concepts. In C++, Namespaces are one of the most useful concepts. What are Namespaces in&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":84616,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3405],"tags":[4206,4207,4208,4209],"class_list":["post-84001","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpp","tag-c-namespaces","tag-discontinuous-namespace-in-c","tag-need-for-namespaces-in-c","tag-nested-namespace-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C++ Namespaces with examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn about namespaces in C++ &amp; how to define them with rules to follow. See the three ways in which you can use namespaces in C++.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/cpp-namespaces\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Namespaces with examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn about namespaces in C++ &amp; how to define them with rules to follow. See the three ways in which you can use namespaces in C++.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/cpp-namespaces\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-10T03:30:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/C-Namespaces.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ Namespaces with examples - TechVidvan","description":"Learn about namespaces in C++ & how to define them with rules to follow. See the three ways in which you can use namespaces in C++.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/cpp-namespaces\/","og_locale":"en_US","og_type":"article","og_title":"C++ Namespaces with examples - TechVidvan","og_description":"Learn about namespaces in C++ & how to define them with rules to follow. See the three ways in which you can use namespaces in C++.","og_url":"https:\/\/techvidvan.com\/tutorials\/cpp-namespaces\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-09-10T03:30:20+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/C-Namespaces.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-namespaces\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-namespaces\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"C++ Namespaces with examples","datePublished":"2021-09-10T03:30:20+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-namespaces\/"},"wordCount":645,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-namespaces\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/C-Namespaces.jpg","keywords":["C++ Namespaces","Discontinuous Namespace in C++","Need for namespaces in C++","Nested Namespace in C++"],"articleSection":["C++ Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/cpp-namespaces\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-namespaces\/","url":"https:\/\/techvidvan.com\/tutorials\/cpp-namespaces\/","name":"C++ Namespaces with examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-namespaces\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-namespaces\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/C-Namespaces.jpg","datePublished":"2021-09-10T03:30:20+00:00","description":"Learn about namespaces in C++ & how to define them with rules to follow. See the three ways in which you can use namespaces in C++.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-namespaces\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/cpp-namespaces\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-namespaces\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/C-Namespaces.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/C-Namespaces.jpg","width":1200,"height":628,"caption":"C++ Namespaces"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-namespaces\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"C++ Namespaces with examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/84001","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=84001"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/84001\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/84616"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=84001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=84001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=84001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}