Placing the using Directive in Multifunction Programs

Notice that Listing 2.5 places a using directive in each of the two functions:

using namespace std;

This is because each function uses cout and thus needs access to the cout definition from the std namespace.

There’s another way to make the std namespace available to both functions in Listing 2.5, and that’s to place the directive outside and above both functions:

// ourfunc1.cpp -- repositioning the using directive#include <iostream>using namespace std; // affects all function definitions in this filevoid simon(int);int main(){    simon(3);    cout << "Pick an integer: ";    int count;    cin >> count;    simon(count);    cout << "Done!" << endl;    return 0;}void simon(int n){    cout << ...

Get C++ Primer Plus now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.