Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

Using the Standard Namespace

You’ll notice that the use of std:: in front of both cout and endl becomes rather distracting after a while. Although using the namespace designation is good form, it is tedious to type. The ANSI standard allows two solutions to this minor problem.

The first is to tell the compiler, at the beginning of the code listing, that you’ll be using the standard library cout and endl, as shown on lines 5 and 6 of Listing 2.3.

Listing 2.3. Using the using Keyword

  1:  // Listing 2.3 - using the using keyword
  2:  #include <iostream>
  3:  int main()
  4:  {
  5:      using std::cout;  // Note this declaration
  6:      using std::endl;
  7:
  8:      cout << "Hello there.\n";
  9:      cout << "Here is 5: " << 5 << "\n";
 10:      cout << "The manipulator endl ";
 11:      cout << "writes a new line to the screen.";
 12:      cout <<  endl;
 13:      cout << "Here is a very big number:\t" << 70000;
 14:      cout <<  endl;
 15:      cout << "Here is the sum of 8 and 5:\t";
 16:      cout << 8+5 << endl;
 17:      cout << "Here's a fraction:\t\t";
 18:      cout << (float) 5/8 << endl;
 19:      cout << "And a very very big number:\t";
 20:      cout << (double) 7000 * 7000 << endl;
 21:      cout << "Don't forget to replace Jesse Liberty ";
 22:      cout << "with your name...\n";
 23:      cout << "Jesse Liberty is a C++ programmer!\n";
 24:      return 0;
 25:  }

Output

Hello there.
Here is 5: 5
The manipulator endl writes a new line to the screen.
Here is a very big number:      70000
Here is the sum of 8 and 5:     13
Here's a fraction:              0.625
And a very very big number:     4.9e+007
Don't forget to replace Jesse Liberty with your name...
Jesse Liberty is a C++ programmer!

Analysis

You will note that the output is identical to the previous listing. The only difference between Listing 2.3 and Listing 2.2 is that on lines 5 and 6, additional statements inform the compiler that two objects from the standard library will be used. This is done with the keyword using. After this has been done, you no longer need to qualify the cout and endl objects.

The second way to avoid the inconvenience of writing std:: in front of cout and endl is to simply tell the compiler that your listing will be using the entire standard namespace; that is, any object not otherwise designated can be assumed to be from the standard namespace. In this case, rather than writing using std::cout;, you would simply write using namespace std;, as shown in Listing 2.4.

Listing 2.4. Using the namespace Keyword

  1:  // Listing 2.4 - using namespace std
  2:  #include <iostream>
  3:  int main()
  4:  {
  5:      using namespace std;       // Note this declaration
  6:
  7:      cout << "Hello there.\n";
  8:      cout << "Here is 5: " << 5 << "\n";
  9:      cout << "The manipulator endl ";
 10:      cout << "writes a new line to the screen.";
 11:      cout <<  endl;
 12:      cout << "Here is a very big number:\t" << 70000;
 13:      cout <<  endl;
 14:      cout << "Here is the sum of 8 and 5:\t";
 15:      cout << 8+5 << endl;
 16:      cout << "Here's a fraction:\t\t";
 17:      cout << (float) 5/8 << endl;
 18:      cout << "And a very very big number:\t";
 19:      cout << (double) 7000 * 7000 << endl;
 20:      cout << "Don't forget to replace Jesse Liberty ";
 21:      cout << "with your name...\n";
 22:      cout << "Jesse Liberty is a C++ programmer!\n";
 23:      return 0;
 24:  }

Analysis

Again, the output is identical to the earlier versions of this program. The advantage to writing using namespace std; is that you do not have to specifically designate the objects you’re actually using (for example, cout and endl;). The disadvantage is that you run the risk of inadvertently using objects from the wrong library.

Purists prefer to write std:: in front of each instance of cout or endl. The lazy prefer to write using namespace std; and be done with it. In this book, most often the individual items being used are declared, but from time to time each of the other styles are presented just for fun.

  • + Share This
  • 🔖 Save To Your Account