C++ Tutorial
C++ Namespaces
Namespaces keep names from colliding. std is the one you already use.
Two libraries, one name
C++ programs pull in many headers. Your code might define count. A library might also definecount. If both names live in the global space, the compiler cannot tell them apart. A namespace is a labeled box around names so stats::count and ui::count can both exist.
You have been using a namespace since the first program: std. cout, string, and vector live there. using namespace std; opens the box so you can write the short names. The names were never global to begin with.
std::cout without using namespace
The qualifier is two colons: std::cout. That means “the cout that belongs tostd.” This program skips using namespace std; entirely. Every standard name is spelled out. The program still compiles and still prints.
Example
#include <iostream>
int main() {
int n = 5;
std::cout << n << std::endl;
return 0;
}Paste this into /cpp/try. If you write bare cout here, g++ will say it was not declared. The using-directive was what made the short name work on earlier pages.
A namespace of your own
Write namespace, a name, and a brace block. Functions and types inside belong to that namespace. Call them with the same :: syntax you use for std. This mathx box holds one function so it cannot clash with some other square in a larger project.
Example
#include <iostream>
using namespace std;
namespace mathx {
int square(int n) {
return n * n;
}
}
int main() {
cout << mathx::square(5) << endl;
cout << mathx::square(12) << endl;
return 0;
}The function is not square in the global space. It is mathx::square. Other files can define a different square without colliding, as long as they sit in a different namespace or you keep yours qualified.
Two boxes, same short name
This program puts label in two namespaces. Both functions return a string. The calls pick which one with ::. Without namespaces, two global functions named label would not compile together.
Example
#include <iostream>
#include <string>
using namespace std;
namespace audio {
string label() {
return "track";
}
}
namespace video {
string label() {
return "frame";
}
}
int main() {
cout << audio::label() << endl;
cout << video::label() << endl;
return 0;
}using, one name at a time
using namespace std; imports every name in std. That is convenient in a short.cpp file and is the house style for most StudyGrid examples. In a larger file it can hide collisions until a new include appears.
You can import one name instead: using std::cout;. Then cout is short, butendl still needs std::endl unless you using that too. This tutorial's small programs keep the blanket using-directive. Qualifying everything, as in the first example, is the safest default when you leave the tutorial.
| Write | What you get |
|---|---|
std::cout | One qualified name. No using-directive required. |
using std::cout; | Short cout only. |
using namespace std; | Short names for the whole of std. |
mathx::square(5) | Your function, explicitly. |
Do not wrap main
int main() stays in the global namespace. Do not put main insidenamespace. The runtime looks for the global main as the start of the program.
Namespace names follow the same rules as other names: letters, digits, underscore, no spaces. Pick a short word that is unlikely to match a library: a project prefix, not std and not string.
Qualify when two meanings exist
If a name appears in one place, a short call is fine. If two namespaces offer the same name, write:: every time and skip a using-directive for those boxes. Next: destructors, which run when an object leaves scope and is destroyed.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C++ editor at /cpp/try. Change one measurement and check whether the result still has the right unit.
Physics
g in a lab namespace
Standard gravity and the gravitational constant G are easy to mix up. A namespace lab::g is 9.81, not 6.674×10⁻¹¹. The :: is the point of the chapter.
W = m g
Example
#include <iostream>
using namespace std;
namespace lab {
const double g = 9.81;
}
int main() {
cout << "weight = " << 70.0 * lab::g << " N" << endl;
return 0;
}Chemistry
Avogadro in chem
One mole is 6.022×10²³ specified entities. Half a mole of anything is about 3.011×10²³ of those entities. Putting NA in namespace chem keeps it off a variable named na that meant sodium.
N = n × N_A
Example
#include <iostream>
using namespace std;
namespace chem {
const double NA = 6.022e23;
}
int main() {
double moles = 0.5;
cout << "molecules ~ " << moles * chem::NA << endl;
return 0;
}