sponsor

sponsor

Slider

Recent Tube

Business

Technology

Life & style

Games

Sports

Fashion

» » Namespaces

Introduction for Namespaces

Namespace ये variable, function और classe का एक समूह होता है |
जैसे class को class keyword के साथ नाम दिया जाता है उसी तरह namespace keyword के साथ namespace का नाम दिया जाता है |
अगर एक ही program में एक ही variable को दो बार declare किया जाए तो redeclaration का error आ जाता है | लेकिन इन एक जैसे variable को एक ही program में अलग-अलग namespaces में declare किया जा सकता है |
Namespace पर आखिर में class की तरह semicolon(;) नहीं दिया जाता |
Namespace को function के अन्दर define नहीं किया जाता इसका मतलब namespace का scope global होता है |

Namespace की विशेषताए

  • Namespace के आखिर में class की तरह semicolon(;) नहीं दिया जाता |
  • Namespace का object नहीं बनाया जाता |

Syntax for Namespace

namespace namespace_name{
 
 // body of namespace;

}

अगर namespaces के data को access करना हो तो ,
Syntax for Accessing Namespace members
namespace_name :: member(); or member(arguments_list);

Example for Namespace

#include <iostream.h>
using namespace std;

namespace ns1{            //Namespace 1
   void show(int x){
       int a = x;
      cout<<"Namespace 1"<<endl;
      cout<<"Value of a : "<<a<<endl;
}
}
namespace ns2{            //Namespace 2
   void show(){
      cout<<"Namespace 2"<<endl;
}
}

int main () {

ns1::show(6);   //Accessing Methods of Namespace
ns2::show();

return 0;
}
Output:
Namespace 1
Value of a : 6
Namespace 2

अगर एक ही namespace को access करना हो तो function को main() function में call करना काफी होता है |
for eg.
 
int main () {

show();

return 0;
}

Namespaces को alias name भी दिया जाता है |
For eg.
namespace m = ns1;    // alias name of ns1 is m
namespace n = ns2;    // alias name of ns2 is n
int main () {

m::show(6);
n::show();

return 0;
}

Nested Namespace

Namespace nested type का भी होता है |
Nested Namespace के members को access करना हो तो scope resolution का इस्तेमाल किया जाता है |
अगर ns1 के member को access करना हो तो,
using namespace ns1;   // ns1 namespace member accessing
int main () {

show();
return 0;
}

अगर ns2 के member को access करना हो तो,
using namespace ns1 :: ns2;   // ns2 namespace member accessing
int main () {

show();
return 0;
}

Example for Nested Namespace

Source Code :
#include <iostream.h>
using namespace std;

namespace ns1{
   void show(){
      cout<<"Namespace 1"<<endl;
}
namespace ns2{
   void show(){
      cout<<"Namespace 2"<<endl;
}
}
}
using namespace ns1 :: ns2; //ns2 namespace member accessing
int main () {

show();
return 0;
}
<> Output:
Namespace 2

Anonymous/Unnamed Namespace

Namespaces बिना नाम के भी define किये जा सकते है |
लेकिन multiple namespaces के variable, function और class अलग-अलग होते है |
Unnamed Namespace में ऐसा नहीं की उसे कोई नाम नहीं होता, Compiler की तरफ से उसे एक unique नाम दिया जाता है |

Syntax for Unnamed Namespace

namespace{
 
 //some_code;
 
}

Source Code :
#include <iostream.h>
using namespace std;

namespace{
   void show(){
      cout<<"Namespace 1"<<endl;
}
}
namespace{
   void get(){
      cout<<"Namespace 2"<<endl;
}
}
int main () {

show();
get();

return 0;
}
Output:
Namespace 1
Namespace 2

«
Next
Newer Post
»
Previous
Older Post

No comments:

Leave a Reply