Tuesday, July 28, 2009

I hv found in some places of C++ programs (using namespace std;) at the begining of the program.why we use dis

using namespace std; in which compiler we used or in every program but when I was learnt this language at that I never use it.why n where we used it n what happens if we not use or use it.I work in Turbo C++

I hv found in some places of C++ programs (using namespace std;) at the begining of the program.why we use dis
Consider the following program


int main()


{





std::cout%26lt;%26lt;"something that you want";





return 0;


}





this is the format of writing when we do not declare


using namespace std;





When we declare this then we don't have to write


std:: before that statement.


consider


using namespace std;


int main()


{


cout%26lt;%26lt;"whatever you want";


return 0;


}


This is addition of ANSI standard.
Reply:dont use it..


works without to..
Reply:Well, namespaces are used to create an intermediate scope in which the variables are visible. this is not compulsory thing, your program will work even if you don't mention your namespace. But it is a good practice to use it.





Think of a situation where you and your friend are working on a same project and are doing two different parts of it. Both of you have some dozens of globals in your parts. Now, by chance some of variables are having similar names. Here when you combine your codes, it will create the problems as now your global spaces are common and both of your variables are in that common space.





A namespace is a solution of this thing. If you and your friend are having your own namespaces then you can have similar names for the variables and still it will not create any problem as you are "using" different namespace scopes.


You can even access your friend's variable using his scope





like your variable will be


myscope::comm_var





and his


hiscope::comm_var





and the problem is solved. Now instead of using variables like this you can switch namespaces using "using namespace" thing. Just search on net for more detailed information.








I hope I have cleared your confusion to some extent. :)
Reply:Let's come up with a scenario that in a program there are two developers, none of them knew what the other doing. Accidently they come up with a same function (method) name. Without namespaces, compiler does not know which function will be used at all if both of them are used in the same place. So the idea of namespaces is to pack functions and classes to a same package then later on you can specify explicitly which one you want to use





Hope you understand my poor English
Reply:You don't have to.





lets say....





#include %26lt;iostream%26gt;





int main()


{


std::cout %26lt;%26lt; "hey there";


}





no matter what you have to include either those two. it is just the syntax of c++.





std means standard.





when you include something, it will be in your code, but it might not work in your main() because it is not standardnized.
Reply:this is programs


No comments:

Post a Comment