Simple Example Program For Namespace In C++
Definition
A namespace (sometimes also called a name scope) is an abstract container or environment created to hold a logical grouping of unique identifiers or symbols (i.e., names). An identifier defined in a namespace is associated only with that namespace. The same identifier can be independently defined in multiple namespaces. That is, the meaning associated with an identifier defined in one namespace may or may not have the same meaning as the same identifier defined in another namespace. Languages that support namespaces specify the rules that determine to which namespace an identifier (not its definition) belongs.
The functionality of namespaces is especially useful in the case that there is a possibility that a global object or function uses the same identifier as another one, causing redefinition errors.
Namespace Syntax
namespace abc {
int variable;
}
Adding Namespace:
using namespace abc;
Usage Namespace Member:
abc::variable
Namespace Example Program
/* Example Program For namespace Example In C++
little drops @ thiyagaraaj.com
Coded By:THIYAGARAAJ MP */
#include <iostream>
using namespace std;
//Namespace namespace first
namespace namespacefirst {
int value = 5;
}
//Namespace namespace second
namespace namespacesecond {
double value = 3.1416;
}
int main() {
//Namespace namespace first Variable Usage
cout << "namespacefirst value : " << namespacefirst::value << endl;
//Namespace namespace second Variable Usage
cout << "namespacesecond value : " << namespacesecond::value << endl;
return 0;
}
Sample Output
namespace first value: 5
namespace second value: 3.1416
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Example Program For Constructor Overloading In C++
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Do While Loop Example Program In C++
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Exception Handling Divide by zero Using C++ Programming