Coupon Accepted Successfully!


Anonymous Namespaces

A namespace without a name is known as an anonymous namespace. The members of anonymous namespaces can be accessed globally in all scopes. The frequent use of such a namespace is to protect global members from same name classes among files. Each file possesses a separate anonymous namespace. The following programs explain the use of namespaces:


21.13 Write a program to create namespace, declare, and access elements.


using namespace std;

namespace num


int n;

void show() {cout<<“\n n=”<<n;}


int main()




return 0;







Explanation: In the above program, the namespace num contains one integer member n and one member function show(). In the function main(), the variable n is initialized to 100. The members are accessed using a scope access operator and the namespace name. The functionshow() displays the value of the variable.

Test Your Skills Now!
Take a Quiz now
Reviewer Name