what is the difference between the "iostream" library and "using namespace std;" in c++

I am new to C++. I can't get the difference between `iostream` library and `using namespace std;` because `iostream` library are two types named `istream` and `ostream`, which represent `input and output streams`, respectively. And `namespace std` is the `standard namespace`.` cout`, `cin` and a lot of other things are defined in it. (This means that one way to call them is by using `std::cout` and `std::cin`.)

So my question is, what is the need of defining both `iostream` and `using namespace std;` in a program?
Sourodip KunduAsked:
Who is Participating?
 
ZoppoCommented:
Hi Sourodip Kundu,

these two statements are completely unrelated.

1.) std is a namespace. Namespaces are used to put related functions together in any meaningful way, i.e. grouped by functionality or by library or anything. See the sample code below to see how it can be used.

Everything which is not declared within any namespace is part of the unnamed, so called global namespace ::. This is i.e. needed to access objects/functions from within a named namespace.

With using namespace a namespace can be made available in the current namespace, this means all functions/objects from such a namespace can be used in the current namespace too.

Important hint: Many experts recomment not to use using namespace std;, at least not on a global scope (not within a functions, a class declaration, or another namespace) for different reasons - there are a lot of discussion in the Internet, i.e. https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice


2.) libraries: Libraries are collection of functions and/or objects which can be used in your program. The default (and most important) library is the STL (Standard Template Library) which offers really much for wide spreaded needs, i.e classes for containers, strings, files, streams, many more, ... and functions/algorithms.

STL is a header-only library, so all you need to do to use it is to include the appropriate headers. JFI: Beside header-only libraries it's even possible to build/use libraries with pre-built binaries (called .LIB).


#include <iostream> // make some STL functions/classes available

void foo() {
 std::cout << "foo() in global namespace called\n";
}

namespace my {
  using namespace std;
  void foo()  { cout << "my::foo() called\n"; } // no 'std::' needed with 'cout' - but see comments about this below
  void bar()  { ::foo(); }
}

int main()
{
  foo(); // calls global ::foo()
  my::foo(); // cally my::foo()
  bar(); // error, there's no global ::bar()
using namespace my;
  bar(); // ok, namespace 'my' is known now
}

Open in new window

This is a very short and simplified description, I hope it helps anyway.

Best regards,

ZOPPO
0
 
Sourodip KunduAuthor Commented:
Thank you, I got it
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.