namespace in cpp 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: c++ custom namespace

//using namespaces
using namespace std;

//creating namespaces
namespace custom{
  class example{
    public:
    	static int method(){
          return 0;
        }
  };
};

//using custom namespaces
using namespace custom;

Example 3: what is namespace in c++

//namespace is a declarative region to provide scope for identifiers
#include <bits/stdc++.h>

using namespace std; //including namespace std for cin and cout
//my custom namespace for variables and functions
namespace abc
{
  void fun()
  {
    cout<<"Hello world"<<endl;
  }
  int x=10;
}
using namespace abc;
int main()
{
  cout<<10;
  fun();
  return 0;
}

Example 4: 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> {};
}

Tags:

Cpp Example