what are namespaces in c++ code example
Example 1: using namespace std in c++
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World";
system("pause");
return 0;
}
Example 2: what is namespace in c++
#include <bits/stdc++.h>
using namespace std;
namespace abc
{
void fun()
{
cout<<"Hello world"<<endl;
}
int x=10;
}
using namespace abc;
int main()
{
cout<<10;
fun();
return 0;
}
Example 3: access the namespace members using namespace member function
namespace Parent
{
inline namespace new_ns
{
template <typename T>
struct C
{
T member;
};
}
template<>
class C<int> {};
}
Example 4: access the namespace members using namespace member function
#include <string>
namespace Test
{
namespace old_ns
{
std::string Func() { return std::string("Hello from old"); }
}
inline namespace new_ns
{
std::string Func() { return std::string("Hello from new"); }
}
}
#include "header.h"
#include <string>
#include <iostream>
int main()
{
using namespace Test;
using namespace std;
string s = Func();
std::cout << s << std::endl;
return 0;
}