In this article using namespace std we give the information about with the help of namespaces, you can separate the code, this makes it easy to manage the code and errors do not come.
C++ provides you the ability to define your own namespaces and use them in your program. With the help of namespaces, you can separate the code, this makes it easy to manage the code and errors do not come.
using namespace std:
Introduction to C++ Namespaces
Suppose more than one programmer is working on a project, then it may happen that two programmers create classes or functions with the same name. In this situation, when the code of all programmers will be executed simultaneously, then due to having classes or functions of the same name, an error will occur in the project. To avoid this situation, C++ provides you the feature of namespace.
Introduction to Namespaces
Namespace is a group of variables, functions and classes.
Just as a class is named with the class keyword, similarly a namespace is named with the namespace keyword.
If the same variable is declared twice in the same program, then redeclaration error occurs. But these same variables can be declared in different namespaces in the same program.
A semicolon (;) is not given at the end of a namespace like a class.
Namespace is not defined inside the function; it means that the scope of namespace is global.
Namespace Features
The semicolon (;) is not given at the end of the namespace like the class.
Namespace object is not created.
Syntax for Namespace
namespace namespace_name
{
// body of namespace;
}
If you want to access the data of namespaces,
Syntax for Accessing Namespace members
namespace_name :: member(); or member(arguments_list);
Example for Namespace
#include <iostream.h>
using namespace std;
namespace ns1
{ //Namespace 1
void show(int x)
{
int a = x;
cout<<“Namespace 1″<<endl;
cout<<“Value of a : “<<a<<endl;
}
}
namespace ns2
{
//Namespace 2
void show()
{
cout<<“Namespace 2″<<endl;
}
}
int main ()
{
ns1::show(6); //Accessing Methods of Namespace
ns2::show();
return 0;
}
Output:
Namespace 1
Value of a : 6
Namespace 2
If only one namespace is to be accessed, calling the function in the main() function is sufficient.
for eg.
int main ()
{
show();
return 0;
}
Namespaces are also given alias names.
For eg.
namespace m = ns1; // alias name of ns1 is m
namespace n = ns2; // alias name of ns2 is n
int main () {
m::show(6);
n::show();
return 0;
}
Nested Namespace
Namespace is also of nested type.
If the members of Nested Namespace are to be accessed then scope resolution is used.
If you want to access the member of ns1,
using namespace ns1; // ns1 namespace member accessing
int main ()
{
show();
return 0;
}
If you want to access the member of ns2,
using namespace ns1 :: ns2; // ns2 namespace member accessing
int main ()
{
show();
return 0;
}
Example for Nested Namespace
Source Code :
#include <iostream.h>
using namespace std;
namespace ns1{
void show(){
cout<<“Namespace 1″<<endl;
}
namespace ns2{
void show(){
cout<<“Namespace 2″<<endl;
}
}
}
using namespace ns1 :: ns2; //ns2 namespace member accessing
int main () {
show();
return 0;
}
Output:
Namespace 2
Anonymous/Unnamed Namespace
Namespaces can also be defined without a name.
But the variable, function and class of multiple namespaces are different.
In Unnamed Namespace it is not that it does not have any name, it is given a unique name by the Compiler.
Syntax for Unnamed Namespace
Namespace
{
//some_code;
}
Source Code :
#include <iostream.h>
using namespace std;
namespace
{
void show()
{
cout<<“Namespace 1″<<endl;
}
}
Namespace
{
void get()
{
cout<<“Namespace 2″<<endl;
}
}
int main ()
{
show();
get();
return 0;
}
Output:
Namespace 1
Namespace 2
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining