i want to know that #include <iostream> it included std. input output library and
using namespace std also included standard lib. then why we have to write both things? please explain it in detail!
iostream didnt include std. it gives the user the abiltity to bring anything contained in the std namespace (in that file at least) into scope with the command using.
Including a header gains you access to any objects/functions/classes/etc that are defined in that header. For example, when you #include <iostream> , you gain access to std::cout, std::cin, std::ostream, etc, etc.
Notice how all of that has the 'std::' prefix. That is because it is all defined within the std namespace:
1 2 3 4 5 6 7 8
// a fully functional example program
#include <iostream> // for std::cout, std::endl
int main()
{
std::cout << "hello world" << std::endl;
return 0;
}
What usingnamespace std; does... is it takes everything in the std namespace and dumps it into the global namespace (effectively destroying the entire point of having a separate namespace).
This lets you access objects without the std:: prefix (ie.. you can just type cout instead of std::cout) but increases the likelihood of name conflicts in your program... since you might accidentally name a function/class the same thing as something already in the std namespace.