Namespaces are logically separated sections of a program. They are required if you want multiple functions with the same name. These functions can be declared in two different namespaces and called by referencing the corresponding namespace.
It is similar to referring to the second names of two people who share the same first name. So namespace instructs the compiler to use one of the two identically named functions.
Assume you have two functions named calculate(), each of which performs a different task. One calculate() function performs multiplication, while another performs addition. To avoid ambiguity, you will declare both functions in separate namespaces in this case. These namespaces will distinguish both functions and provide information about both functions.
As shown below, a namespace definition begins with the keyword namespace, followed by the namespace name −
namespace namespace_name {
// code declarations
}
int main()
{
// main function
}
There are three ways to use a namespace in the program,
#include <iostream>
using namespace std;
// first name space
namespace first_space {
void func() {
cout << "Inside first space" << endl;
}
}
// second name space
namespace second_space {
void func() {
cout << "Inside second space" << endl;
}
}
int main () {
// Calls function from first name space.
first_space::func();
// Calls function from second name space.
second_space::func();
return 0;
}
Inside first_space
Inside second_space
You can use using keyword to import an entire namespace into your program with a global scope. It can import a namespace into another namespace or any program.
#include <iostream>
using namespace std;
// first name space
namespace first_space
{
void func()
{
cout << "Inside first_space" << endl;
}
}
// second name space
namespace second_space
{
void func()
{
cout << "Inside second_space" << endl;
}
}
using namespace first_space;
int main ()
{
// This calls function from first name space.
func();
return 0;
}
Inside first_space
When we use a directive, we import all the names in the namespace and make them available throughout the program, implying that they have a global scope.
However, when we use declaration, we import one specific name at a time, which is only available within the current scope.
#include <iostream>
using namespace std;
// first name space
namespace first_space
{
void func()
{
cout << "Inside first_space" << endl;
}
}
// second name space
namespace second_space
{
void func()
{
cout << "Inside second_space" << endl;
}
}
using namespace first_space;
int main ()
{
using namespace first_space; // using directive
using second_space::func; // using declaration
func(); // calls func() of second_space namespace
first_space::func(); // class func() of first_space namespace
return 0;
}
Inside second_space
Inside first_space
Note: also read about Multithreading in C++
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…