5
In C++ because some use std::cout
and others use only cout
is just what the programmer thinks best?
5
In C++ because some use std::cout
and others use only cout
is just what the programmer thinks best?
10
std::cout
is the full name of the object, including its "family name", the surname. A lot of people like to use it like this, eliminating any ambiguity.
Others prefer to use only the object name to make it shorter. It’s a more informal way of using. To be possible it is necessary to indicate first which family to use. The main one of C++ is precisely the std
. So making:
using namespace std;
dispenses with the use of the surname in each object of the code belonging to the family std
.
See more in Because it is not good practice to use namespace "Std" in C++? to understand the advantages and disadvantages of it. But in essence it is taste.
7
In C++ all names defined in global scope belong to a namespace.
A name is in the global scope when it is declared outside a function and a class. For example
#include <iostream>
int a = 1;
int main()
{
int b = 2;
std::cout << "a=" << a << " b=" << b << "\n";
{
int c = 3;
std::cout << "a=" << a << " b=" << b << " c=" << c << "\n";
}
}
In the above example, the variable a
is in the global scope (we say it is a "global variable"), and the variables b
and c
are in a local scope (we say they are "local variables"). The local variable b
is within the scope of the function main
, and the local variable c
is in the scope of block unlocked {
previous, and locked by key }
coming up next.
Well, all that was to say that all the names that are in the global scope belong to a namespace. A namespace, as the name says, is a "namespace", or a "set of names".
When a name in the global scope does not explicitly belong to a namespace, then automatically the name belongs to the global namespace. Therefore, in the above example, the global variable a
belongs to the global namespace.
To indicate that a name in the global scope belongs to a namespace, we need to give the name of the namespace to which this name belongs.
#include <iostream>
int a = 1;
namespace x
{
int a = 2;
};
int main()
{
int b = 3;
std::cout << "a=" << a << " b=" << b << "\n";
{
int c = 3;
std::cout << "a=" << a << " b=" << b << " c=" << c << "\n";
}
std::cout << "a=" << a << " x::a=" << x::a << "\n";
}
In the example above we have two variables a
global: one belonging to the global namespace (initialized with 1) and one belonging to the namespace x
(initialized with 2).
Note that to reference the variable a
belonging to the namespace x
we need to do this explicitly, through the notation x::a
.
There are two ways to simplify coding and not keep repeating the namespace name. The two ways use the directive using
.
The first way simplifies the reference to only one namespace name. In the example above, just after declaring the namespace x
we would use the directive using x::a;
. In this way, we could refer to the variable x::a
just writing a
. However in the above example this will not work as it will create conflict with the variable a
(initialized with 1) belonging to the global namespace!
In the following example the variable x::y
is referenced in this simplified manner.
#include <iostream>
int a = 1;
namespace x
{
int a = 2;
int y = 2;
};
using x::y;
int main()
{
int b = 3;
std::cout << "a=" << a << " b=" << b << "\n";
{
int c = 3;
std::cout << "a=" << a << " b=" << b << " c=" << c << "\n";
}
std::cout << "a=" << a << " x::a=" << x::a << "\n";
std::cout << "x::y=" << y << "\n";
}
The other way to simplify access to variables belonging to a namespace is by using the "using namespace" directive. This way all names belonging to the namespace can be referenced without needing to explicitly put the name of the namespace.
The example below illustrates the use of using namespace
(note the namespace z
).
#include <iostream>
int a = 1;
namespace x
{
int a = 2;
int y = 2;
};
using x::y;
namespace z
{
int aa = 1;
int bb = 2;
};
using namespace z;
int main()
{
int b = 3;
std::cout << "a=" << a << " b=" << b << "\n";
{
int c = 3;
std::cout << "a=" << a << " b=" << b << " c=" << c << "\n";
}
std::cout << "a=" << a << " x::a=" << x::a << "\n";
std::cout << "x::y=" << y << "\n";
std::cout << "z::aa=" << aa << " z::bb=" << bb << "\n";
}
Okay, and what does all this have to do with std::cout
? The answer is: all classes and global objects in the standard C++ language library are declared within the namespace std
.
So, to reference a class or an object from the standard C++ library we need to specify that they belong to the namespace std
.
Then, we can use one of the 3 ways described above: explicitly specify the namespace (as in std::cout
), or use a drain for each name in std
(using std::cout
), or use a single directive for all names on std
(using namespace std
).
The third way, using namespace std
, is not advised as it frees access to all the names of the namespace std
, leaving room for potential conflict.
However, the use of using namespace std
in small programs, sample programs, test programs, etc., it’s all a matter of "common sense".
To complete, we can explicitly refer to a variable in the global namespace through notation ::
. For example, ::a
is a reference to the name a
declared in the global namespace.
(Edit. Still missing talk about anonymous namespace, but this is next.)
Browser other questions tagged c++ namespace cout
You are not signed in. Login or sign up in order to post.
To use only
cout
you need to use namespace:using namespace std;
. C++ standard library resources are declared within the namespacestd
. Read more on C++ Standard Library and Name visibility.– KelvinS