can't understand first program of c++

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.

Hope this helps.
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: using namespace 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.
Last edited on
I use an old text book to study C++. It has the header #include <iostream.h> and I don't have to use using namespace std or std::cout. to get cout <<.

Which is better ? #include <iostream.h> simply or as you suggest the newer version #include <iostream> together with using namespace std ??
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.
Topic archived. No new replies allowed.