Loading....
Coupon Accepted Successfully!

 

Namespace

Namespace is one of the new features introduced by the ANSI C++.

It is used by programmers to avoid name clashes when programmer uses more than one library. If duplicate name with a matching scope is found in two parts of the program then name clash occurs.

All the items within the namespace have public visibility and the items declared within the namespace must include the C++ standard library and directives like using namespace std;.

Syntax of namespace is very similar to the classes or struct. The keyword namespace is followed by a namespace name, an opening curly brace, and terminated with closing brace without

semicolon.
Syntax of the namespace is as follows-
 

Namespace name_of_the_namespace

{

…………………………

………………………….

………………………….

}


4.23 Write a program to use namespace keyword and access variables by using scope resolution operator.

 

Note: Following program is executed using visual c++.

 

Explanation: In the above program, variables v and f are declared within the namespace and are accessed by using scope resolution operator (::).


4.24 Write a program to perform few arithmetic operations between the variables declared under two namespaces.

 

Note: Following program is executed using visual c++.

 

Explanation: In the above program, two different namespaces are used. Variables declared within them are used for performing the mathematical operations such as addition and multiplication.
 

4.25 Write a program to illustrate nested namespaces. Also use the unnamed namespace and display the variable declare and initialized in it.
 

Note: Following program is executed using visual c++.

 

Explanation: In this program nesting of namespaces is used and the variables declared under it are accessed. Unnamed namespace is used and variable within it is assigned a value which is displayed.


4.26 Write a program to declare a function using namespace and perform different operations.

 

Explanation: Two functions named as mul and div are taken in namespace. Arguments to the functions are passed from the main() and functions’ results are displayed.





Test Your Skills Now!
Take a Quiz now
Reviewer Name