0% found this document useful (0 votes)
26 views

Using Namespace: // My Second Program in C++

The document discusses namespaces in C++ and how elements in the std namespace can be accessed either by explicitly qualifying each use with std:: or by using a using declaration to introduce visibility of the namespace components without needing to qualify each element. It provides an example of rewriting code to use cout without the std:: prefix by adding a using namespace std; declaration. Both approaches are valid in C++ but using declarations improve readability while explicit qualification avoids potential name collisions.

Uploaded by

elly_popa7182
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Using Namespace: // My Second Program in C++

The document discusses namespaces in C++ and how elements in the std namespace can be accessed either by explicitly qualifying each use with std:: or by using a using declaration to introduce visibility of the namespace components without needing to qualify each element. It provides an example of rewriting code to use cout without the std:: prefix by adding a using namespace std; declaration. Both approaches are valid in C++ but using declarations improve readability while explicit qualification avoids potential name collisions.

Uploaded by

elly_popa7182
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

space: the namespace std.

In order to refer to the elements in the std namespace a program shall either qualify each and
every use of elements of the library (as we have done by prefixing cout with std::), or introduce
visibility of its components. The most typical way to introduce visibility of these components is by
means of using declarations:

using namespace std;


The above declaration allows all elements in the std namespace to be accessed in
an unqualified manner (without the std:: prefix).
With this in mind, the last example can be rewritten to make unqualified uses of cout as:

1 // my second program in C++


2 #include <iostream>
3 using namespace std;
4
5 int main ()
6{
7 cout << "Hello World! ";
8 cout << "I'm a C++ program";
9}

Hello World! I'm a C++ program

Both ways of accessing the elements of the std namespace (explicit qualification
and using declarations) are valid in C++ and produce the exact same behavior. For simplicity, and
to improve readability, the examples in these tutorials will more often use this latter approach
with using declarations, although note that explicit qualification is the only way to guarantee that
name collisions never happen.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy