In C++, namespaces are used to organize too many classes so that it can be easy to handle the application. Namespace is an essential feature that offers a method to group related identifiers (such as variables, functions, classes, methods, and many more) to avoid name collisions. In C++ , we need to use namespacename::classname to access the class of a namespace. We can also use the "using" namespace keyword so that we don't have to use complete names all the time.
Syntax
It has the following syntax:
namespace namespace_name {
// declarations
}
A namespace can be defined using the namespace keyword that is followed by the namespace name.
Accessing Members of Namespace
If we want to access the namespace members in C++, we need to use the scope resolution operator.
Syntax
It has the following syntax:
Namespace_name::Member_name
C++ Namespace Example
Let's see the simple example of the namespace, which includes variables and functions.
Example
#include <iostream>
using namespace std; //using standard namespace
namespace First {
void sayHello() {
cout<<"Hello First Namespace"<<endl;
}
}
namespace Second {
void sayHello() {
cout<<"Hello Second Namespace"<<endl;
}
}
int main() //Main Function
{
First::sayHello();
Second::sayHello(); //Accessing Members using scope resolution operator
return 0;
}
Output:
Hello First Namespace
Hello Second Namespace
Explanation
In this example, we define two namespaces, First and Second, each of them contains a sayHello function. In the main function, both functions are accessed using the scope resolution operator (First::sayHello and Second::sayHello), which allows clear and unambiguous function calls from each namespace.
Namespace with using keyword
The using directive enables us to take an entire namespace from another program file into the program. If we do not want to write the namespace every time when we need to access the variable in the program, we can utilize the using keyword.
Syntax
It has the following syntax:
using namespace namespace_name;
C++ Namespace example using the Using Directive
Let us take an example to illustrate the namespace using the "using" directive in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
namespace First{
void sayHello(){
cout << "Hello First Namespace" << endl;
}
}
namespace Second{
void sayHello(){
cout << "Hello Second Namespace" << endl;
}
}
using namespace First;
int main () { //main function
sayHello();
return 0;
}
Output:
Hello First Namespace
Explanation
In this example, we define two namespaces, First and Second, each with a sayHello function. By using "using namespace First;", the compiler calls First::sayHello in main. After that, the Second::sayHello is not accessible without full qualification or additional using directive.
In-built Namespaces
There are several in-built namespaces in C++. Some main functions in C++ in-built namespace are as follows:
Global Namespace
The global namespace is the root namespace in C++. Any identifier declared outside of a named namespace belongs to the global namespace. By default, if we don-t specify a namespace, variables , functions , and classes are considered part of the worldwide namespace.
In C++, we can use the :: (scope resolution operator) without any preceding namespace name to explicitly refer to the global namespace inside another namespace.
Syntax
It has the following syntax:
::identifier // Access global variable, function, or class
C++ Global Namespace Example
Let us take an example to illustrate the global namespace in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
int value = 10; // Global namespace variable
namespace cpptutorial {
int value = 20;
void show() {
cout << "Local namespace value: " << value << endl;
cout << "Global namespace value: " << ::value << endl;
}
}
int main() { //main function
cpptutorial::show();
return 0;
}
Output:
Local namespace value: 20
Global namespace value: 10
Explanation
In this example, we illustrate the implementation of namespaces and scope resolution. Next, a global variable value is defined with a value of 10, and inside the cpptutorial namespace, another value is defined with a value of 20. After that, the show function displays both values: value from the cpptutorial namespace and the global value using the scope resolution operator (::).
Using the std Namespace
In C++, the std (standard) namespace contains all the classes and functions of the Standard Library, including cout, cin, string, vector, etc. It allows us to prevent the naming conflicts with user-defined identifiers.
If we want to use the elements from the std namespace, we can use the following steps.
1) Use the scope resolution operator (std::)
Example: std::cout << "Hello";
2) Use the using directive (using namespace std;)
It allows direct access without std::, but it can lead to name conflicts.
3) Use using declarations (using std::cout;)
It selectively imports specific items from the namespace.
C++ std Namespace Example
Let us take an example to illustrate the std namespace in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
int main() { //main function
cout << "Hello from std namespace" << endl;
return 0;
}
Output:
Hello from std namespace
Explanation
In this example, we display a simple message using the standard namespace. By including using namespace std;, it enables us to direct use of cout and endl without prefixing them with std::.
Nested Namespaces
In C++, nested namespaces allow us to define a namespace inside another namespace. It is useful when organizing code hierarchically, particularly in big projects. It helps to avoid name conflicts and improves code readability.
Syntax
It has the following syntax:
namespace Outer::Inner {
// body of the code
}
C++ Nested Namespace Example
Let us take an example to illustrate the nested namespace in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
namespace Company { //namespace outer
namespace Project { //namespace inner
void showDetails() {
cout << "Welcome to the Company Project" << endl;
}
}
}
namespace Tech::Development {
void greet() {
cout << "Welcome to the Development Department" << endl;
}
}
int main() { //Main function
// Accessing functions from nested namespaces
Company::Project::showDetails();
Tech::Development::greet();
return 0;
}
Output:
Welcome to the Company Project
Welcome to the AI Department
Explanation
In this example, we demonstrate nested namespaces using both traditional and C++17 simplified syntax. After that, the Company::Project::showDetails function is defined using the traditional nested namespace, while Tech::Development::greet uses the compact C++17 syntax. Both functions are accessed using scope resolution in the main function.
C++ Namespace MCQs
1) Which of the following options is utilized to signify the namespace?
- Scope Resolution Operator
- Conditional Operator
- Bitwise Operator
- Ternary Operator
2) What will be the output of the following code?
#include <iostream>
using namespace std; //using standard namespace
namespace first
{
int var = 8;
}
namespace second
{
double var = 4.1754;
}
int main () //Main Function
{
int a;
a = first::var + second::var;
cout << a;
return 0;
}
- Compilation Error
3) Which of the following directive is utilized for namespace in C++?
- including namespace
- accessing namespace
- with namespace
- using namespace
4) Which of the following statements is correct about namespace in C++?
- A namespace is mainly utilized to group classes, objects, and functions.
- A namespace is utilized to separate the class and objects.
- A namespace is mainly utilized to mark the start and end of the program.
- A namespace is mainly utilized to mark the start of the program.
5) What will be the output of the following code?
#include <iostream>
using namespace std; //using standard namespace
namespace Box1
{
int x = 20;
}
namespace Box2
{
int y = 18;
}
int main () //Main function
{
int x = 10;
Box1::x;
Box2::y;
cout << x;
return 0;
}