C++ Standard Library, Headers and Namespaces Overview

Category : Articles
C++ Standard Library, Headers and Namespaces Overviewby Adriene Blaison.C++ Standard Library, Headers and Namespaces OverviewC++ Standard Library “std” is an abbreviation for standard and the standard library namespace in C++. Defined in it is useful functionality for cout, cin, string and much more. You can call or reference this functionality by using std::cout, std::cin and std::string but that’s more typing and code to write for each use. A library […]

C++ Standard Library

std” is an abbreviation for standard and the standard library namespace in C++. Defined in it is useful functionality for cout, cin, string and much more. You can call or reference this functionality by using std::cout, std::cin and std::string but that’s more typing and code to write for each use. A library is a bunch of classes, functions, headers, etc. that help you in your development by giving you access to code and functionality that you didn’t write.

Using and Namespaces

The “using” keyword means use this (in our case “std”) when you can to locate “cout”, etc. therefore with the using statement (“using namespace std;”) std::cout will be located when you type only cout. If you only typed and used cout without the using namespace std statement or prefixing with “std::” then cout would not be able to be located even though you included iostream because inside iostream its referenced or lives in a namespace std therefore its scoped to the std namespace. Instead you would receive an error because technically without including the namespace that function or functionality doesn’t exist to the compiler.

The “using” keyword does not add functionality but rather references or reads it and brings it into scope otherwise it would be invisible without a direct reference (“std::cout”). It’s the include statement that loads and adds functionality from what we include as in the case of iostream including functionality for cout, cin, etc. You can think of an include statement like including a Google Map (the code/header file (e.g. iostream)) with many destinations (code) and the namespacestd” as an address or location on the map (identifying where the code exists).

Namespaces are a way to organize code, avoid ambiguity, protect against name duplication, and give it scope. In order for code that’s written outside the namespace to see and reference code inside the namespace the namespace must be referenced hence “using namespace std;” or “std::cout” in the case of cout.

Putting it all Together

Therefore when you include the library code (C++ code file/header file for the functionality you want to include) you’re including the functionality but that functionality is scoped and lives inside a namespace that you need referenced so that code can be located hence we include the library to have the code as part of our program and then reference the namespace so we can use that specific code.

iostream contains code for cout, cin, etc. In order to use that functionality that code must be included in our program and that’s the purpose of the include statement whereas the namespace can be thought of as a location identifier.

Real Life Example

Let’s think of another real world example. In order to go hiking I need my backpack and it can be thought of like iostream where iostream contains useful code that I didn’t write my backpack contains useful items that I didn’t manufacture. My backpack has several hidden pockets that I know exist with different items and these can be thought of as a namespace or location. I ask my friend to retrieve my flashlight from my backpack. In order to do that he needs my backpack and also a pocket location in order to get my flashlight. In order for us to work with cout we need to include the code and reference its location. When we include iostream it will include the header/C++ code file with all this useful code we didn’t write ourselves and load it as part of our program. The code/header files are included in the C++ language and the path is operating system (OS) dependent and different.

Strings and another Example

The string header or code file is included for and working with variables of type string. In order to declare and work with string variables in C++ we must include the string code/header file and reference the code via the std namespace. This is another example of either referencing it as “std::string” or including “using namespace std;” to then only use “string” inside your code. Including “using namespace std;” allows you to avoid typing the namespace portion to use “string” same as in the case of “cout”, “cin”, etc. The “std::string” class in C++ is a standard representation of a text string and you must include the code/header file for string and the std namespace in order to use it. That’s the way the language was designed, developed and has evolved.

Conclusion 

As a C++ beginner it’s probably enough to be aware and know that you need to include certain things and write certain pieces of code in order to use certain functionality. As a beginner learning C++ it’s important to become comfortable knowing what code you need to write, when and what to include to gain access to certain functionality like cout.

Take the C++ Course

Content Excerpt from the course “How to Program in C++ from Beginner to Paid Professional” on Udemy (discount applied for those interested in learning more about C++ in this great course).

Share Button

Related Posts