using
Declaration Is Required for Each NameEach using
declaration introduces a single namespace member. This behavior lets us be specific about which names we’re using. As an example, we’ll rewrite the program from § 1.2 (p. 6) with using
declarations for the library names it uses:
#include <iostream>
// using declarations for names from the standard library
using std::cin;
using std::cout; using std::endl;
int main()
{
cout << "Enter two numbers:" << endl;
int v1, v2;
cin >> v1 >> v2;
cout << "The sum of " << v1 << " and " << v2
<< " is " << v1 + v2 << endl;
return 0;
}
The using
declarations for cin
, cout
, and endl
mean that we can use those names without the std::
prefix. Recall that C++ programs are free-form, so we can put each using
declaration on its own line or combine several onto a single line. The important part is that there must be a using
declaration for each name we use, and each declaration must end in a semicolon.