Programming Language & Its Applications β C++ Constructs with Objects and Classes, NEC licence examination syllabus (Nepal Engineering Council).
A named scope for identifiers β the fix for the name collisions that plagued large C programs.
using namespace std;, and almost every experienced C++ programmer will tell you not to write it. That disagreement is worth understanding rather than memorising. Namespaces exist because C had no way to stop two libraries from both defining log() β and when that happened, the linker simply failed with a duplicate-symbol error and you could not use both libraries in one program. Java's package, Python's modules and Rust's mod all solve the same problem the same way. Search "why using namespace std is considered bad practice" once you finish this page; the answer involves std::count versus your own count, and it will make sense.#include <iostream>
namespace graphics {
int resolution = 1080;
void draw() { std::cout << "drawing a triangle\n"; }
}
namespace audio {
int resolution = 44100; /* same NAME, no clash */
void draw() { std::cout << "drawing a waveform\n"; }
}
int main() {
graphics::draw(); audio::draw();
std::cout << "video " << graphics::resolution
<< "p, audio " << audio::resolution << " Hz\n";
return 0;
}
Output:
drawing a triangle
drawing a waveform
video 1080p, audio 44100 Hz
#include <iostream>
#include <vector>
namespace company {
namespace project { /* nested */
int version = 3;
void info() { std::cout << "v" << version << "\n"; }
}
}
/* C++17 shorthand for the same nesting: */
/* namespace company::project { ... } */
int main() {
/* 1. fully qualified */
company::project::info();
/* 2. using declaration - one name only */
using std::cout;
cout << "cout works bare; ";
std::cout << "std::cout still fine\n";
/* 3. using directive - scoped to this block */
{
using namespace company::project;
info(); /* no prefix needed HERE */
}
/* info(); <-- error out here: not declared */
/* 4. alias */
namespace cp = company::project;
cout << "via alias: " << cp::version << "\n";
return 0;
}
Output:
v3
cout works bare; std::cout still fine
v3
via alias: 3
#include <iostream>
#include <algorithm> /* declares std::count */
/* A perfectly reasonable global of my own */
int count = 0;
int main() {
/* With "using namespace std;" at file scope, this
line becomes AMBIGUOUS: ::count or std::count? */
count = 5; /* fine WITHOUT the using directive */
std::cout << "count = " << count << "\n";
/* The scoped, safe habit: */
using std::cout; /* just the names you need */
cout << "explicit is better\n";
return 0;
}
Output:
count = 5
explicit is better
/* Add "using namespace std;" above and some compilers
report:
error: reference to 'count' is ambiguous
candidates: int count / std::count(...) */
using inside your function costs only you. A using in your header costs everyone who includes it, invisibly, and they cannot undo it. That asymmetry β local convenience versus imposed global cost β is a design principle worth carrying into every API you ever write.
draw in different namespaces (and overloading, next topic), the linker cannot work with plain names. So the compiler encodes the namespace, class and parameter types into the symbol: graphics::draw() becomes something like _ZN8graphics4drawEv. Run nm --demangle on any C++ binary and you can read it back. This is also exactly why you need extern "C" to call C++ code from C. Search "C++ name mangling extern C".::, and state the difference between a using declaration (one name) and a using directive (all names). Know that namespaces can nest and be aliased, that an unnamed namespace gives internal linkage, and that adding names to std is forbidden. A short program with two same-named functions in different namespaces is the standard question.Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Loadingβ¦