Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟢 Foundation  ·  Lesson 12

Namespaces in C++

In this lesson: What namespaces are in C++, why using namespace std, custom namespaces and scope — with output. In Hindi & English.

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.
← 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.
Next topicHeader Files in C++ Your progress: 12 / 81 (15%) · Lesson 13 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free