Namespace in c++ with example program in hindi , what is Namespace in c++

what is Namespace in c++  ,Namespace in c++ with example program in hindi :-

इससे पहले के article मे Namespace के  CONCEPT को discuss किया है अब इस article मे namespece के सार को पढेगे जिससे आप सभी को इस concept को अच्छी तरह से समज सके |

दैनिक जीवन मे , कई बार ये हो जाता है जब किसी class मे एक ही नाम से दो student है तब in student को differentiateकरने के लिए in student के नाम के अलावा दूसरी information को use किया जाता है |जैसे इसका location , father ‘s name |

इस तरह की situation c++ language मे अरिसे हो सकता है | जैसे किसी प्रोग्राम मे कसीस code segment को function abc() से define किया जाता है | लेकिन किसी दुसरे file मे same name से function को define किया जाता है | तब complier confuse हो सकता है |

namespace को इस difficulty को दूर करने के लिए use किया जाता है | जिसमे  समान नाम के functions, classes, variables etc. की additional information को contain करता है जो की इस functions, classes, variables etc. को दुसरे functions, classes, variables etc. से अलग करता है |namespace मे functions, classes, variables etc. के name के scope को hold किया जाता है |

Defining a Namespace

namespace definition , keyword namespace से शुरू होता है इसका उदाहरन निन्म है :-

namespace namespace_name

{

// code declarations

}

किसी  namespace-enabled version मे से function or variable को call करने के लिए  (::) use किया जाता है :-

namespace_name :: function_name ;  // code could be variable or function.

namespace_name :: variable_name  ;  // code could be variable .

Let us see how namespace scope the entities including variable and functions −

source code

#include <iostream>

using namespace std;

// first name space

namespace first_namespace {

void function() {

cout << “Inside first_namespace” << endl;

}

}

// second name space

namespace second_namespace {

void function() {

cout << “Inside second_namespace” << endl;

}

}

int main () {

// Calls function from first name space.

first_namespace::function();

// Calls function from second name space.

second_namespace::function();

return 0;

}

जब इस code को execute किया जाता है तब इसका आउटपुट होगा :-

Inside first_namespace

Inside second_namespace

The using directive

namespace के  prepending को avoid किया जाता है | namespace directive complier को define करता है की किसी code मे names को use किया जाता है | इस fact को निन्म प्रोग्राम मे add किया जाता है :-

Source Code

#include <iostream>

using namespace std;

// first name space

namespace first_namespace {

void function() {

cout << “Inside first_namespace” << endl;

}

}

// second name space

namespace second_namespace {

void function() {

cout << “Inside second_namespace” << endl;

}

}

using namespace first_namespace;

int main () {

// This calls function from first name space.

function();

}

जब इस प्रोग्राम को run किया जाता है तब इसका आउटपुट निन्म होता है :

Inside first_namespace

‘using’ directive को किसी namespace के particular name को use करने के लिए किया जाता है | जब cout जो की namespace std का भाग है को use करने के लिए निन्म पैटर्न को use किया जाता है :-

using std::cout;

इस function मे केवल cout को use किया जा सकता है लेकिन दुसरे  item को use करने के लिए इस code को सभी individual item के लिए use किया जाता है |

Source code

#include <iostream>

using std::cout;

int main () {

cout << “std::endl is used with std!” << std::endl;

return 0;

}

जब इस code को complie किया जाता है  तब इसका आउटपुट होता है :-

If we compile and run above code, this would produce the following result −

std::endl is used with std!

using directive मे declare name namespace के rules को फॉलो करता है |  किसी  name पुरे प्रोग्राम मे तब तक active रहता है जब तक इसका scope को declare किया जाता है | समान name की variable , function को इस scope के बहार declare करना होता है |

Discontiguous Namespaces

किसी namespace को किसी प्रोग्राम मे कई बार declare किया जता है | इसलिए namespace को अलग अलग parts को merge करने के लिए किया जाता है | किसी namespaceमे declare name को पुरे प्रोग्राम मे use किया जा सकता है |

अगर किसि  namespace मे से name को दूसरी file मे से use करने के लिए name को declare करने के लिए किया जाता है | नीचे दिए गये उदाहरन में एक नए namespace को declare किया जाता है जिसमे नए name को declare किया जाता है और इस name को add भी किया गया है |

namespace namespace_name {

// code declarations

}

Nested Namespaces

किसिस namespace के body मे एक और namespace को भी declare किया जा सकता है इसका syntax निन्म होता है :-

namespace namespace_name1 {

// code declarations

namespace namespace_name2 {

// code declarations

}

}

किसी namespace मे से name को access करने के लिए  resolution operators को use किया जाता है | इसका syntax निन्म होता है |

// to access members of namespace_name2

using namespace namespace_name1::namespace_name2;

// to access members of namespace:name1

using namespace namespace_name1;

उपर वाले syntax मे resolution operators name1 को declare किया जाता है जिसमे resolution operators name2 के element को भी declare किया गया है |

जिसका source code निन्म है :

Source code

#include <iostream>

using namespace std;

// first name space

namespace first_namespace {

void function() {

cout << “Inside first_namespace” << endl;

}

// second name space

namespace second_namespace {

void function() {

cout << “Inside second_namespace” << endl;

}

}

}

using namespace first_namespace::second_namespace;

int main () {

// This calls function from second name space.

function();

return 0;

}

 जाब इस function को use किया जाता है जिसका आउटपुट निन्म होता है :-

 Inside second_namespace

 इस article मे namespace के सभी concept को सार सहित declare किया गया है आब आगे के article मे c++ language मे कुछ advance topic को discuss करेगे |