{ namespace } Keyword in C++ Programming Language | BCA IGNOU C++ TUTORIAL

Ashutosh Kumar
By -
0


Hello guys, 


We will going to the basic use of namespace keyword under C++ Programming Language. As we know that C++ is OOP programming language. So, it support encapsulation like properties. That is implemented to hide some information. For example, In C++  during class declaration we use  private modifier to make that class member identifiers to be not accessed publically.


Namespace can be used to organize the class, function & variable for specific task.


Namespace provide a local scope to C++ variable, function and class. 


Example of Namespace in C++


int main(){

int value;
value=0;

double value; // Error here
value=0.00;
}

Compiler Output:

'value' has a previous declration as 'int value'


Reasion:::

In each scope, a name can only represent one entity. So, there cannot be two value with with the same name in the same scope. { }



By using Namespace, we can solve the problem

#include <iostream>

using namespace std;

namespace first {  int val = 500; } int val=100; // Global scoped //To access or execute of name spaced variable  cout << first::val ; // 500 cout << val;         // 100

( :: ) - This operator is know as resolution operator.
 
You may have observed the difference b/w above two example that How the namespace keyword, can give power to use to declare the same identifier names with different context that has been declared in outer scope.


Namespace
  • It should be declared in Global scope.
  • It doesn't contain access modifier keywords like public and private.
  • It can be use to declare variable, function and class in itself.
  • We can declare namespace in namespace and can be access with Resolution Operator ( :: ).

Post a Comment

0Comments

✍️ Only article/post centric comments are allowed.
❌**No Spam**
❌**Advertisement**
❌**No abuse**
Rules strictly applied ✅

Post a Comment (0)