P
Peter van Merkerk
soni29 said:hi,
i did some c++ back in college, currently work mainly with java, and
i'm trying to refresh myself in c++, after starting a tutorial online
i noticed that the author used the line using namespace std. what is
that? i've never seen that in college or in my c++ text. i see the
site has the #include<iostream.h> to bring in cin and cout but not
sure why the std line is there. the code the site had is:
#include <iostream.h>
#include <string>
using namespace std;
int main() {
string name;
cout << "What is your name?";
cin >> name;
cout << "Welcome, " << name << ".";
return 0;
}
Namespaces are used to avoid name clashes with other libraries. C++
namespace are _somewhat_ similar to Java packages. Standard library
class and functions reside in the std namespace. Without the 'using
namespace' directive you would have to explicitly add std:: before every
identier of the standard library:
int main() {
std::string name;
std::cout << "What is your name?";
std::cin >> name;
std::cout << "Welcome, " << name << ".";
return 0;
}
The 'using namespace std;' directive pulls the identifiers in the std
namespace into the current namespace so that you don't have to
explicitly specify that you are refering to a identifier from the std
namespace. The 'using namespace' directive should be used judiciously in
the smallest posible scope and never in header files. To liberal use of
'using namespace' directives may lead to unexpected results.
BTW. <iostream.h> is non-standard header it should be <iostream>. If the
tutorial can't get these simple things right one might wonder about the
quality and accuracy of rest of the tutorial