Loading....
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.

#include<iostream>

using namespace std;

namespace num

{

int n;

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

}

int main()

{

num::n=100;

num::show();

return 0;

}

 

OUTPUT

n=100

 

 

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