what is the difference between

std::cout<<"dfdfd"<<endl;//everywhere  

and

using namespace std::cin

putting above line at the top of the main function

and putting the below line at the top of the main

using namespace std;

and + 2 bonus questions.

1.Are there any other namespaces than 'std' in c++?
2. Are cin and cout the only ones in the namesapce 'std'?

try to answer all if you can.thanks

Recommended Answers

All 5 Replies

Rather than trying to explain, I'll link you to a article that talks about namespace. In general for simple question you should try to be more independent. Take care.

  1. There are plenty namespaces in c++, manly because everyone can create a namespace:

    namespace something{
    
        //insert your classes, variables, and code here
    
    }
    
  2. cin and cout are few of the items from the std namespace. Basically std namespace conains all the classes and the implementations of the standard C++, meaning it will come with your compiler/package, not requring you to actually implement things.

Other things:

using namespace std;

says to the compiler that you are applying the namespace std to every function that you work with, and is not been implemented by you/you use a standard include

using namespace std::cin;

means that you apply the namespace std only to the cin function:

//code
    cin>>variable1;
    std::cout<<varaible1;
//more code

sorry, it is only (without the 'namespace')

using std::cin

If

All the files in the C++ standard library declare all of its entities within the std namespace,

(above sentence is on http://www.cplusplus.com)
then why is there even a namespace? Becasue all are in one namespace,there won't be any redeclaration problems. It is not like there is another otally different 'cin/cout' in another name sapce. so why is there a std ?

then why is there even a namespace?

That's there so that YOU can use your own namespaces. In large programs, you may face name conflicts (especially if you're just a part of a 100 developer team!). To avoid that you may have custom namespaces for convenience.

so why is there a std ?

Well the C++ standard committe decided to put all the standard objects (variables and stuffs) in one organisation level (that's the std namespace) rather than scattering them over different namespaces like std1, std2, ..., etc.
Their reason was to just show us the feature existed, the real use comes when you use it for your own contexts.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.