-6

I am currently working in c++, I want to know everything that using namespace std adds. I already know the basic ones, like cout and cin. However when I run my program without using namespace std, It doesn't work (I do add std:: before the cout command). But I am wondering what else I need to add std:: before.

Any help would be greatly appreciated!

Here is my code:

#include <iostream>
#include <string>

class MyClass{
public:
    void setName(string x){
        name = x;
    }
    string getName(){
        return name;
    }
private:
    string name;
};

int main()
{
    MyClass mc;
    mc.setName("WASSSSSUUUPP!!! \n");
    std::cout << mc.getName();
}
killarviper
  • 165
  • 1
  • 1
  • 8

1 Answers1

2

1 - All the entities (variables, types, constants, and functions) of the standard C++ library are declared within the std namespace. using namespace std; introduces direct visibility of all the names of the std namespace into the code.

ref: http://www.cplusplus.com/doc/tutorial/namespaces/

2 - Namespaces in C++ are most often used to avoid naming collisions. Although namespaces are used extensively in recent C++ code, most older code does not use this facility. For example, the entire C++ standard library is defined within namespace std, but before standardization many components were originally in the global namespace.

ref: http://en.wikipedia.org/wiki/Namespace#Use_in_common_languages

garakchy
  • 554
  • 10
  • 19
  • 1
    Dr. Stroustrup recommends avoiding namespace usage in header files. – Vector Jun 23 '14 at 01:20
  • @Vector, I agree, [that is considered bad](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – garakchy Jun 24 '14 at 00:14