what is the necessity of "namespace" in c++
Hi all!
I would like know the purpose of "namespace"... many of programs are having .. "using namespace std" as their first line in main()...........
Now my question is why this namespace should be used? what is the real use? without writing this namespace statement also i am able to run the programs.
i have studied as "Namespaces allow to group entities like classes, objects and functions under a name" but i could not understand the usage, suppose if i did not allow to group the entities what should be happened? please let me have a clarification.
Thanks in advance:
regards:
raghunadhs
Re: what is the necessity of "namespace" in c++
C++ Code:
#include <iostream.h>
int main() {
std::cout<<"Hello World!"<<endl;
return 0;
}
vb Code:
#include <iostream.h>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}
See in the first example how you have to use std::cout well you would have to do that everytime you use methods and members in the std namespace. Where as in the second example you will not have to use std:: before any member ;)
You can make your own namespaces to :)
vb Code:
namespace shop{
int item;
int price;
}
//....
using namespace shop;
so you can do
instead of
Hope that helped a little :D
Re: what is the necessity of "namespace" in c++
but while i was doing my degree, i used c++.at that time i did not use neither
std:cout<<" " nor "using namespace std", what is the reason ...? i think it a newly added feature in ANSI C++, is it right? if so, suppose, in ANSI C++ , i did not use any "using namespace std" statement, will it give any error? since i am not using C++(just i know how to program in c++), i don't have much idea about the recent editions.pls clarify this doubt?
[QUOTE=Hell-Lord]
C++ Code:
#include <iostream.h>
int main() {
std::cout<<"Hello World!"<<endl;
return 0;
}
[HIGHLIGHT=vb]
Re: what is the necessity of "namespace" in c++
You may have been using C and not C++.
Re: what is the necessity of "namespace" in c++
Yes Hell-Lord,
actually, now i am not using C++, i am working on v.b. but i want to know this for genereal idea.
Quote:
Originally Posted by Hell-Lord
You may have been using C and not C++.
Re: what is the necessity of "namespace" in c++
The biggest usage of namespaces is to prevent naming conflicts.
For example, suppose I am working on a large project, and I have the following classes:
class Object
class Car
class Truck
class Dog
and a large amount of code that uses/implements these objects. Now my coworker shows me that his part of the project also has a class named Object and a class named Dog. The compiler is not going to appreciate having multiple classes with the same name.
If my coworker encloses all his types in the namespace "Coworker" and I put all my classes in the namespace "MyStuff", as long as the names are properly qualified when used, there is no conflict.
Obviously this example is a little contrived, but you get the idea :)
Re: what is the necessity of "namespace" in c++
..in addition, they are also used for grouping logically related pieces of code/data.
chem
Re: what is the necessity of "namespace" in c++
Hi sunburnt!
Excellent, explenation... i am happy with this explenation.
Thanks:
regards:
raghunadhs.