C++ is one of the most versatile languages in the world. It is used nearly everywhere for everything… systems programming (operating systems, device drivers, database engines, embedded, Internet of Things, etc.)

Enter Header Image Headline Here

Thursday, 13 April 2017

C++ Name space creation

                                    Namespace creation


Using namespace we can have class global variables function under one name : We can change global scope to Sub-scope.

Namespace is a container for identifiers. it puts the names of its members in a distinct space so that they don't conflict with the names in other namespaces or global namespace.




  • Namespace is a logical compartment used to avoid naming collisions.
  • The naming conflict or collision always Occur-
  • When same program is using more than one library with some variables or functions having same name.
  • The name collision may occur for global variable, global functions, classes etc.
  • Default namespace is global namespace and can access global data and functions by proceeding (::) operator.
  • We can create our own namespace. And naything declred within namespace has scope limited to namespace.

Creating a namespace :

  • Creation of namespace is similar to creation of class.
  • Namespace declartions can be nested within another namespace.
  • Namespace declarations don't have access specifiers. (public or private)
  • Namespace declaration appear only at global scope.
  • No need to give semicolon after the closing brace of definition of namespace.
  • We can split the defination of namespace over several units.


Syntax : 

Live Example


































What is Unnamed Namespace  :

  • Unnamed namespaces are the replacement for the static declaration of variables.
  • In unnamed namespaces usable in the samespace in not mentioned in the declaration of namespace.
  • They are directly usable in the sme program and are used for declaring unique indentifiers.
  • The name of the namespace is uniquely generated by the compiler.
  • The unnamed namespaces you have created will only be accessible within the file you created it in.












Output :


How to Use Name space in C++ Programming Language :


Following are the ways to refer to namespace members:

Way 1 :  Using Scope Resolution :

In this method, we use the namespace name, scope resolution operator (::) and member of that namespace.

Way 2: Using Directive: 

We can use 'using' directive to specify the namespace.







































Way 3: Using declaration :

It is a declaration within the current scope. this means it can override names from a using directive.
























 Output :







0 comments:

Post a Comment

Popular Posts

Recent Posts

Categories

Unordered List

Text Widget