Friday, February 27, 2015

What is namespace?

Question:What is namespace??

Answer: Namespaces allow us to group a set of global classes, objects and/or functions under a name. To say it somehow, they serve to split the global scope in sub-scopes known as <I>namespaces</I>.&nbsp; The form to use <I>namespaces</I> is:&nbsp; <TT><B>namespace </B><I>identifier</I></TT> <TT><B>{</B></TT> <TT>&nbsp;&nbsp;<I>namespace-body</I></TT> <TT><B>}</B></TT>Where <TT><I>identifier</I></TT> is any valid identifier and <TT><I>namespace-body</I></TT> is the set of classes, objects and functions that are included within the <I>namespace</I>. For example:&nbsp; <PRE>namespace general </PRE><PRE>{ </PRE><PRE>&nbsp;&nbsp;int a, b; </PRE><PRE>} </PRE>In this case, <TT><B>a</B></TT> and <TT><B>b</B></TT> are normal variables integrated within the <B>general</B> <I>namespace</I>. In order to access to these variables from outside the namespace we have to use the scope operator <TT><B>::</B></TT>. For example, to access the previous variables we would have to put:&nbsp; <TT>general::a</TT>&nbsp; <TT>general::b </TT>The functionality of <I>namespaces</I> is specially useful in case that there is a possibility that a global object or function can have the same name than another one, causing a redefinition error.

Tags:Interview Questions, Languages, C++,

Please submit your Interview Answer for the above Question via replying to this post. Thank you for visiting to our blog, Have a Good Day!.



Sponsor Advertisement



Customer Relationship Management Software

Customer Resource Management Software

CRM software is designed to help businesses in manageing their customer data and customer interaction, access business information, automate sales, marketing and customer support and also manage employee, vendor and partner relationships. ...

No comments:

Post a Comment