using namespace std

hello guys
why do we use using namespace std; in a program??(i read the link:http://www.cplusplus.com/doc/tutorial/program_structure/)but didnt understand.
and if we do why did'nt we included in the programs of sumita arora of XI class for eg in a program:
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
cout<<"\nWelcome to c++\n";
getch();
}
thanks in advance.(*_*)
1
2
3
4
5
6
7
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
cout<<"\nWelcome to c++\n";
getch();
}


This code is not C++. It's not even old C++, thanks to that void main(). If your compiler accepts this, get a C++ compiler instead. Whatever compiler you're using to compile this probably doesn't even recognise the word namespace.

We use namespaces because it helps us keep all the functions and variables well-organised, and lets us create functions and variables with the same names without the compiler complaining about it. Sticking using namespace std; at the top of your code trashes all that, but it's common because it's easy and simple and requires no understanding on the part of the coder.

why did'nt we included in the programs of sumita arora

Bad news time - Sumita Arora's books contain very old and just plain wrong C++. This is unfortunate if that's your class text, as you will not be assessed on your C++ knowledge, but on your knowledge of what Arora thinks is C++.
Last edited on
hey man how can i just forget what i learnt in past years...
you tell me
I'm not saying it's going to be easy to forget wrong things you have been taught. You have my sympathy. At least now you know. You can start by getting a modern C++ compiler that will tell you when you write something that isn't C++.
Last edited on
man but my teacher teaches me that way only...
and presently i dont have time to go extra......
Then teach your teacher how to program in c++ :D
Because I had a basic C# background when I first started C++, I completely understand what you're going through.

However, Moschops is right. void main() is illegal in C++. Visual Studio's cl and several other old compilers support it, but that doesn't make it right. Sorry. :(

Every time you type it, you'll have to catch yourself and remember that it's int main(). I'm sure that if you're using a fairly recent compiler, there'll be a switch to turn it off.

Either that, or you can switch to a compiler that won't compile your code unless it's int main(). Code::Blocks comes bundled with such a compiler. :)

-Albatross
Last edited on
Moschops is right.

You know, I don't hear that nearly enough :p
I didn't say right about everything. :P

Usually, though, when I'm reiterating a point someone made before me, I like to acknowledge that person.

-Albatross
right about everything.

Even better.
Strawman, Moschops. You are misrepresenting my stance by quote mining, ye infernel nutcase. Therefore, I'm afraid I'll have to admit that Moschops is wrong. Cheers. ;)

-Albatross
La la la can't hear you can't hear you. Here, have a cupcake.
Topic archived. No new replies allowed.