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.(*_*)
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 usingnamespace 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++.
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++.
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. :)
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. ;)