Results 1 to 8 of 8

Thread: what is the necessity of "namespace" in c++

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    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

  2. #2
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: what is the necessity of "namespace" in c++

    C++ Code:
    1. #include <iostream.h>
    2.  
    3. int main() {
    4. std::cout<<"Hello World!"<<endl;
    5. return 0;
    6. }

    vb Code:
    1. #include <iostream.h>
    2.  
    3. using namespace std;
    4.  
    5. int main() {
    6. cout << "Hello World!" << endl;
    7. return 0;
    8. }

    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:
    1. namespace shop{
    2. int item;
    3. int price;
    4. }
    5.  
    6. //....
    7.  
    8. using namespace shop;

    so you can do

    vb Code:
    1. item = 5;

    instead of
    vb Code:
    1. shop::item = 5;

    Hope that helped a little

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    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:
    1. #include <iostream.h>
    2.  
    3. int main() {
    4. std::cout<<"Hello World!"<<endl;
    5. return 0;
    6. }

    [HIGHLIGHT=vb]

  4. #4
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: what is the necessity of "namespace" in c++

    You may have been using C and not C++.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    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++.

  6. #6
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    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
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  7. #7
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: what is the necessity of "namespace" in c++

    ..in addition, they are also used for grouping logically related pieces of code/data.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Re: what is the necessity of "namespace" in c++

    Hi sunburnt!
    Excellent, explenation... i am happy with this explenation.
    Thanks:
    regards:
    raghunadhs.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width