Loading....
Coupon Accepted Successfully!

 

Using directive

The keyword using can be used for a declaration as well as a directive. The using directive provides access to all variables declared within the namespace. When using the directive method, we can directly access the variable without specifying the namespace name. The following example illustrates this:

 

21.14 Write a program to create namespace, declare, and access elements. Use using directive method.

#include<iostream>

using namespace std;

namespace num{

int n;

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

}

int main()

{

using namespace num;

n=100;

show();

return 0;

}

 

OUTPUT

n=100

The Keyword using

 

 

Explanation: In the above program, the namespace name is created with a one-integer variable n function show(). The statement using namespace num allows direct access to members of the namespace. Hence, in the function main(), n = 100 initializes n with 100 andshow() displays the value of n on the screen.

Using a declaration

In this method, the keyword using is optional. It is also possible to access a few members of the namespace directly outside the namespace. The following program explains the above points:

 

21.15 Write a program to use declaration method of namespace and access variables.

#include<iostream>

using namespace std;

namespace num

{

int n;

void show()

{cout<<“\n n=”<<n;}

}

int main()

{

//using namespace num;

num::n=100;

num::show();

return 0;

}

 

OUTPUT

n=100;

 

 

Explanation: The above program is similar to the previous one. The only difference is that here the namespace num is not included. In order to access the elements of namespace num, we need to precede the variable name with a scope access operator and a namespace name. The statementnum::n = 100; accesses the variable n and initializes it with 100. In the statement num::show(); the function show() is invoked using the same syntax.

 

21.16 Write a program to use declaration method of namespace and access
variables and functions.

#include<iostream>

using namespace std;

namespace num

{

int n;

void show()

{cout<<“\n n=”<<n;}

}

int main()

{

//using namespace num;

using num::n;

n=100;

num::show();

return 0;

}

 

 

Explanation: In the above program, the statement using num::n; brings the variable n in the current scope; that is, after this statement, it can be accessed directly by its name, for example, n = 100. Here, 100 is initialized to n. However, other variables (other than n) can be accessed by preceding a namespace name before their name such as num::show().

using namespace num;

The above statement allows to access all members of the namespace num directly, and this style of accessing the elements of g namespace is called a using directive.

using num::n;

The above statement allows to access only member variable n of the namespace. The other member cannot be accessed directly. The member other than n can be accessed by specifying the namespace name before the member name as discussed earlier. This style of accessing elements is knows as a using declaration.

 

21.17 Write a program to declare nested namespace and anonymous namespace.

#include<iostream>

using namespace std;

The Keyword using

namespace num

{

int j=200;

namespace num1 {int k=400;}

}

namespace {int j=500;}

void main()

{ cout<<“j=”<<num::j <<“\n”;

cout<<“k=”<<num::num1::k <<“\n”;

cout<<“j=”<<j <<“\n”;

}

 

OUTPUT

j = 200

k = 400

j = 500

 

 

Explanation: In the above program, num and num1 are two namespaces. The num1 is declared inside the namespace num. The last namespace defined is the unnamed namespace. In function main(), the members of the namespaces are accessed using the scope access operator. The variable j is used in two different scopes.

 

21.18 Write a program to declare functions in namespace. Access the function in main().

#include<iostream>

using namespace std;

namespace fun

{

int add (int a, int b) {return (a+b);}

int mul (int a, int b); // prototype declaration

}

int fun :: mul (int a, int b) {return (a*b);}

int main()

{

using namespace fun;

cout<<“\n Addition:” <<add(20,5);

cout<<“\n Multiplication:” <<mul(20,5);

return 0;

}

 

OUTPUT

Addition : 25

Multiplication : 100

 

 

Explanation: In the above program, two functions add() and mul() are declared in namespace fun. In function main(), the statement using namespace fun allows us to access the elements of fun namespace directly. Two integer values are passed to the functions add()and mul(). They return the addition and multiplication of numbers, respectively.

 

21.19 Write a program to declare class in the namespace. Access the member functions.

#include<iostream>

using namespace std;

namespace clas_s

{

class num

{

private:

int t;

public:

num (int m)

{t=m;}

void show()

{cout<<“\n t=”<<t; }

};

}

void main()

{

// indirect access using scope access operator

clas_s ::num n1(500);

n1.show();

// direct access using directive

using namespace clas_s;

num n2(800);

n2.show();

}

 

OUTPUT

t = 500

t = 800

The Standard Namespace std

 

 

Explanation: In the above program, class num is defined in the namespace clas_s. The class has one integer variable t and the member function show(). The member function show() displays the contents of the variable. In the function main()n1 and n2 are objects ofclass num. The data members are initialized using a constructor. The members of class num are accessed with and without the scope access operator. Both the methods of accessing elements are explained in previous programs.





Test Your Skills Now!
Take a Quiz now
Reviewer Name