Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
🟢 Foundation  ·  Lesson 12

Namespaces in C++

Written and reviewed by · Senior IT Faculty · 15+ years’ experience

What is a namespace?

A namespace is a named box for related identifiers. Its job is to prevent name clashes: two libraries can each have a print() without conflict, because one is LibA::print and the other LibB::print. You choose with the scope resolution operator ::.

Defining a namespace

C++
#include <iostream>
using namespace std;
namespace Math {
    int square(int x) { return x * x; }
}
namespace Physics {
    int square(int x) { return x * x * 2; }   // different meaning
}
int main() {
    cout << Math::square(4) << "\n";      // 16
    cout << Physics::square(4) << "\n";   // 32
    return 0;
}
Output:
16
32

The using directive

using namespace X; brings all of X's names into scope; a using declaration brings just one:

C++
using namespace Math;       // all of Math
using Physics::square;      // just this one name
💡 Prefer narrow

A using declaration (using X::name;) is safer than a whole directive, especially in headers.

The std namespace

The entire standard library lives in std — that is why it is std::cout, std::vector, std::string. Writing using namespace std; lets you drop the prefix, handy in small programs but risky in large ones or in headers.

Nested namespaces

C++
namespace App {
    namespace UI {
        void draw() {}
    }
}
// call: App::UI::draw();
// C++17 shortcut: namespace App::UI { void draw() {} }

Anonymous namespaces

A namespace with no name keeps its contents private to the current file — the modern replacement for file-scope static:

C++
namespace {
    int helper() { return 42; }   // visible only in this file
}

Common mistakes

  • using namespace std; in a header, leaking it into every file that includes it.
  • Two using directives that import clashing names, causing ambiguity.
  • Forgetting the :: when a name lives in a namespace.
  • Overusing broad directives instead of targeted declarations.
🏋️ Practice

Create two namespaces Metric and Imperial, each with a distance() function returning different units. Call both with the scope operator, then bring one in with a using declaration.

Summary

  • Namespaces group names to prevent clashes.
  • Select a name with the scope resolution operator ::.
  • using namespace X; imports all; using X::name; imports one.
  • The standard library lives in std.
  • Anonymous namespaces give file-local (internal) linkage.

Namespace क्या है?

Namespace संबंधित identifiers के लिए एक नामित डिब्बा है। इसका काम नाम टकराव रोकना है: दो libraries हर एक का print() बिना संघर्ष हो सकता है, क्योंकि एक LibA::print है और दूसरा LibB::print। आप scope resolution operator :: से चुनते हैं।

Namespace परिभाषित करना

C++
#include <iostream>
using namespace std;
namespace Math {
    int square(int x) { return x * x; }
}
namespace Physics {
    int square(int x) { return x * x * 2; }   // alag arth
}
int main() {
    cout << Math::square(4) << "\n";      // 16
    cout << Physics::square(4) << "\n";   // 32
    return 0;
}
Output:
16
32

using directive

using namespace X; X के सभी नाम scope में लाता है; एक using declaration केवल एक लाता है:

C++
using namespace Math;       // poora Math
using Physics::square;      // sirf yeh ek naam
💡 संकरा पसंद करें

using declaration (using X::name;) पूरे directive से सुरक्षित है, खासकर headers में।

std namespace

पूरी standard library std में रहती है — इसीलिए यह std::cout, std::vector, std::string है। using namespace std; लिखना आपको prefix छोड़ने देता है, छोटे programs में काम का पर बड़े में या headers में जोखिम भरा।

Nested namespaces

C++
namespace App {
    namespace UI {
        void draw() {}
    }
}
// call: App::UI::draw();
// C++17 shortcut: namespace App::UI { void draw() {} }

Anonymous namespaces

बिना नाम का namespace अपनी सामग्री वर्तमान file तक निजी रखता है — file-scope static का आधुनिक विकल्प:

C++
namespace {
    int helper() { return 42; }   // sirf is file me dikhta hai
}

आम गलतियाँ

  • Header में using namespace std;, इसे हर file में रिसाते हुए जो इसे include करे।
  • दो using directives जो टकराते नाम import करें, ambiguity पैदा करते हुए।
  • जब नाम namespace में हो तब :: भूलना।
  • लक्षित declarations के बजाय व्यापक directives का अति-इस्तेमाल।
🏋️ अभ्यास

दो namespaces Metric और Imperial बनाएँ, हर एक में अलग units लौटाने वाला distance() function हो। दोनों को scope operator से call करें, फिर एक को using declaration से लाएँ।

सारांश

  • Namespaces नाम टकराव रोकने को नामों को समूहित करते हैं।
  • Scope resolution operator :: से एक नाम चुनें।
  • using namespace X; सब import करता है; using X::name; एक।
  • Standard library std में रहती है।
  • Anonymous namespaces file-local (internal) linkage देते हैं।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में namespace क्या है?
Namespace एक नामित क्षेत्र है जो संबंधित identifiers — functions, classes, variables — को नाम टकराव रोकने के लिए समूहित करता है। दो libraries दोनों print नामक function परिभाषित कर सकती हैं बशर्ते हर एक अपने namespace के अंदर हो, और आप scope resolution operator से चुनते हैं किसे।
C++ में using namespace std क्या करता है?
using namespace std; standard library के std namespace के सभी नाम scope में लाता है, तो आप std::cout के बजाय cout लिख सकते हैं। यह छोटे programs के लिए सुविधाजनक है पर बड़े code में नाम टकराव पैदा कर सकता है, जहाँ नाम qualify करना या संकरे using declarations सुरक्षित हैं।
C++ में scope resolution operator क्या है?
Scope resolution operator :: किसी विशेष namespace या class से एक नाम चुनता है, जैसे std::cout या MyLib::init()। यह compiler को ठीक बताता है कि आप किस नाम का version चाहते हैं जब वही नाम एक से ज़्यादा scope में हो।
C++ में using directive और using declaration में क्या अंतर है?
using directive, using namespace X;, namespace X से हर नाम लाता है। using declaration, using X::name;, केवल एक विशिष्ट नाम लाता है। Declaration ज़्यादा लक्षित और सुरक्षित है क्योंकि यह वर्तमान scope को namespace की हर चीज़ से नहीं भरता।
C++ में anonymous namespace क्या है?
Anonymous namespace बिना नाम का namespace है, और इसकी सामग्री केवल वर्तमान source file के भीतर दिखती है। यह functions या variables को internal linkage देने का आधुनिक C++ तरीका है, file scope पर पुराने static keyword के इस्तेमाल की जगह।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.