5 New Namespace Program in C++ 2022
Introduction to Namespace:
What do namespaces provide in C++?
“A namespace is a declarative space that provides scope for internal identifiers (names of types, functions, variables, etc.). Namespaces are used to organize code into logical chunks and to avoid call conflicts that can occur, especially if your codebase is made up of multiple libraries.”
What is the namespace in CPP?
The Namespace is used in c++ programs for providing the user define the scope and it has some predefined namespace also which we used in our c++ programs. Like using namespace std, which is used for the standard libraries functions. we are going through some namespace examples in this article.
Below are some namespace in CPP examples.
Syntax of the c++ namespace
namespace namespace_name { // namespace_name's scope //.....Block Statements... //....Variables, Functions Declearation... EXAMPLE - int a,b,c=10; void helloNamespace(){ court<<"Namespace in C++ and the value of c is = "<<c; } }
5 New Namespace Program in C++ 2022
#Namespace Program 1
Program 1 – Using one User Define Namespace in the Program
//.......MyMixIndia.com........ //......#Namespace Program 1....... #include <iostream> using namespace std; // Global variable int var = 20; //.....Making a new namespace with the name "first"..... namespace first { //....integer variable created inside namespace first..... int a = 50; } int main() { //.....Creating a Local variable...... int d; //....Accessing the Global Variable and the First Namespace Variable inside the main function... cout<<"The Global Variable is var = "<<var<<endl; cout<<"The first Namespace Variable is a = "<<first::a<<endl; //......manipulating the first namespace variable with global and local variables...... d = first::a + var; cout<<"Value of Local Variable is d = "<< d <<endl; return 0; }
The output of Program 1
The Global Variable is var = 20 The first Namespace Variable is a = 50 Value of Local Variable is d = 70
Explanation of Program 1
This is a simple example of namespace c++, in this example, there is a global variable var, a new namespace define with the name “first” having a local variable a, another local variable define inside the main function d, then perform an action for adding the first namespace variable “a” (accessing the user define namespace variables by using scope resolution operators::) and the global variable “var” and store them in “d”, and printing the value of “d”.
#Namespace Program 2
Program 2 – Creating Two namespaces in c++ with namespace std
//.......MyMixIndia.com........ //......#Namespace Program 2....... #include <iostream> using namespace std; // Global variable int var = 20; //.....Making a new namespace with the name "first"..... namespace first { //....integer variable created inside namespace first..... int a = 50; } //.....Making a new namespace with the name "second"..... namespace second { //....integer variable created inside namespace second..... int b = 80; } int main() { //.....Creating a Local variable...... int d,e; //....Accessing the Global Variable and the First Namespace Variable inside the main function... cout<<"The Global Variable is var = "<<var<<endl; cout<<"The first Namespace Variable is a = "<<first::a<<endl; cout<<"The Second Namespace Variable is b = "<<second::b<<endl; //......manipulating the first/second namespace variable with global and local variables...... e = second::b + first::a; d = first::a + var; cout<<"Value of Local Variable is e = "<< e <<endl; cout<<"Value of Local Variable is d = "<< d <<endl; return 0; }
The output of Program 2
The Global Variable is var = 20 The first Namespace Variable is a = 50 The Second Namespace Variable is b = 80 Value of Local Variable is e = 130 Value of Local Variable is d = 70
Explanation of Program 2
It is similar to the Explanation of Program 1 but it is having a second namespace as well. And we are performing operations on these namespaces variables with local variables.
#Namespace Program 3
Program 3 – Using one Namespace variable in another Namespace.
//.......MyMixIndia.com........ //......#Namespace Program 3....... #include <iostream> using namespace std; // Global variable int var = 20; //.....Making a new namespace with the name "first"..... namespace first { //....integer variable created inside namespace first..... int a = 50; } //.....Making a new namespace with the name "second"..... namespace second { //....using the first namespace variable in this second namespace..... int c = first::a; //....integer variable created inside namespace second..... int b = 80; } int main() { //.....Creating a Local variable...... int d,e; //....Accessing the Global Variable and the First Namespace Variable inside the main function... cout<<"The Global Variable is var = "<<var<<endl; cout<<"The first Namespace Variable is a = "<<first::a<<endl; cout<<"The Second Namespace Variable is b = "<<second::b<<endl; cout<<"The First Namespace Variable copy in Second Namespace is c = "<<second::c<<endl; //......manipulating the first/second namespace variable with global and local variables...... e = second::b + first::a; d = second::c + var; cout<<"Value of Local Variable is e = "<< e <<endl; cout<<"Value of Local Variable is d = "<< d <<endl; return 0; }
The output of Program 3
The Global Variable is var = 20 The first Namespace Variable is a = 50 The Second Namespace Variable is b = 80 The First Namespace Variable copy in Second Namespace is c = 50 Value of Local Variable is e = 130 Value of Local Variable is d = 70
Explanation of Program 3
Here we can copy the first namespace variable “a” into the second namespace variable “c”, and then add that value in the main function with other local variables and the first namespace variable.
#Namespace Program 4
Program 4 – Using one User Define Namespace in the Program
//.......MyMixIndia.com........ //......#Namespace Program 4....... #include <iostream> using namespace std; //......Making ABC Namespace...... namespace ABC { int var1 = 30; //......Making a add function of no-return type which is adding 2 variables...... void add(int a, int b){ cout<<"\n The Result of a+b is = "<<a+b<<"\n"; } } //......Making DEF Namespace...... namespace DEF { //......Making a add function of no-return type which is adding 3 variables...... void sum(int a, int b, int c){ ABC::add(50,50); cout<<"\n The Result of a+b+c is = "<<a+b+c<<"\n"; } } int main() { //....Callng the Sum function from DEF namespace with ABC namespace function add..... DEF::sum(20,30,40); return 0; }
The output of Program 4
The Result of a+b is = 100 The Result of a+b+c is = 90
Explanation of Program 4
In this program, we have 2 namespaces “ABC” and “DEF” with 2 functions “add” which is having a no-return type, and 2 parameters in ABC and “sum” which have 3 parameters with no- return type. so, can call the add function of ABC namespace inside the sum function of DEF namespace, finally call the sum function inside the main function.
#Namespace Program 5
Program 5 – Calling the return type Function of one namespace into another namespace
//.......MyMixIndia.com........ //......#Namespace Program 5....... #include <iostream> using namespace std; //......Making ABC Namespace...... namespace ABC { //......Making a add function of no-return type which is adding 2 variables...... int add(int a, int b){ return a+b; } } //......Making DEF Namespace...... namespace DEF { int result = ABC::add(20,20); //......Making a add function of no-return type which is adding 3 variables...... int sum(int a, int b){ return a+b+result; } } int main() { //....Callng the Sum function from DEF namespace with ABC namespace function add..... cout<<"The final result sum is = "<<DEF::sum(10,50); return 0; }
The output of Program 5
The final result sum is = 100
Explanation of Program 5
In this Program, we have 2 namespaces and 2 return – type functions “add” and “sum”. we are calling the add function fo ABC namespace inside the DEF namespace and storing that inside result variable of integer type, and further using that value inside the sum function and finally calling the sum function inside the main function of the program.