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.

Frequently Asked Questions

What is a namespace in C++?
A namespace is a named region that groups related identifiers — functions, classes, variables — to prevent name clashes. Two libraries can both define a function called print as long as each is inside its own namespace, and you pick which one with the scope resolution operator.
What does using namespace std do in C++?
using namespace std; brings all names from the standard library's std namespace into scope, so you can write cout instead of std::cout. It is convenient for small programs but can cause name clashes in larger code, where qualifying names or narrower using declarations are safer.
What is the scope resolution operator in C++?
The scope resolution operator :: selects a name from a particular namespace or class, as in std::cout or MyLib::init(). It tells the compiler exactly which version of a name you mean when the same name exists in more than one scope.
What is the difference between a using directive and a using declaration in C++?
A using directive, using namespace X;, brings in every name from namespace X. A using declaration, using X::name;, brings in just one specific name. The declaration is more targeted and safer because it does not flood the current scope with everything from the namespace.
What is an anonymous namespace in C++?
An anonymous namespace is a namespace with no name, and its contents are visible only within the current source file. It is the modern C++ way to give functions or variables internal linkage, replacing the older use of the static keyword at file scope.

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 देते हैं।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.