Dear Sir/Madam,
I have studied basics of c++ two yrs ago.But now when I agian started to study c++ from the beginning.I am not able to understand starting of program
earlier we would use
#include<iostream.h>
in the starting and it was said that iostream.h contains the definetion of functions like cout,cin etc.BUT now we start a program as :-
#include<iostream>
using namespace std;
I am not able to understand the usage of :-
using namespace std;
I have consulted some books but coudn't understand the explanation given in them.
Please solve my problem, I will be obliged.
Regards,
Matanuragi
using namespace std makes stuff like output functions and stuff "visible", or in other words, without the "using namespace std" line, "cout" does not exist unless you type it like std::cout.
Namespaces are used to avoid name collisions between programms and libraries. For example if you declare a function with the same name as a function in a library then you automatically override this function. That's the reason and the usage of namespace. They localize the names of identifiers to avoid name collisions.
To access the functions, variables, classes etc in a namespace you have to use the syntax namespace::variable (variable can be class function etc) which tells the compiler to go to the namespace you provide it and access the specific variable.
The c++ library is defined in the namespace std. And that's the reason you use: usingnamespace std which says the compiler that from this point every function, variable etc in the namespace std is part of your program and you dont need to use the std:: syntax.
The #include <iostream> says to the compiler to include the iostream library which is one of the compiler's default library. It is the same thing as the #include <iostream.h> but now they don't use the ".h" for the standard libraries.
You can use both...
Depends on what you need. If you don't need alot of std functions then you can use the std:: syntax. Or you can just use using std::cout that declares that from this point on your program will only know cout from the std namespace. (You can use any std parameter instead of cout).
It's all up to you.