You are on page 1of 2

Using namespace std

namespaces were explicitly used to resolve the problem of conflicting function or variable names
between the two libraries
#include <iostream>
int main ()
{ std::cout << "Hello world!\n"; return 0; }

OR

#include <iostream>
using namespace std;
int main ()
{ cout << "Hello world!\n"; return 0; }

OR
#include <iostream>
using namespace std;
namespace mystuff
{ int value = 5; }
int main()
{ cout << mystuff::value; //outputs 5 return 0; }

OR
#include <iostream>
using namespace std;
namespace mystuff { int value = 5; }
using namespace mystuff;
int main()
{ cout << value; //outputs 5 return 0; }

OR
Code from library 1:
namespace YoyodyneSockLib
{ int max_connections; int get_connected_state(); int sock_func(); .... };
Code from library 2:
namespace FoobarDBLib { int max_connections; int get_connected_state(); int db_func(); .... };
Programmer's code:
#include "yoyodynesocklib"
#include "foobardblib"
using namespace YoyodyneSockLib;
using namespace FoobarDBLib;
... ...
x = db_func();
y = FoobarDBLib::get_connected_state();
... ...
cout << "max sock connections " << YoyodyneSockLib::max_connections << "\n";
cout << "max database connections " << FoobarDBLib::max_connections << "\n";

You might also like