namespaces c++

C++
// A C++ program to demonstrate use of class 
// in a namespace 
#include <iostream> 
using namespace std; 
  
namespace ns 
{ 
    // Only declaring class here 
    class geek; 
} 
  
// Defining class outside 
class ns::geek 
{ 
public: 
    void display() 
    { 
        cout << "ns::geek::display()\n"; 
    } 
}; 
  
int main() 
{ 
    //Creating Object of geek Class 
    ns::geek obj; 
    obj.display(); 
    return 0; 
} 
// Creating namespaces 
#include <iostream> 
using namespace std; 
namespace ns1 
{ 
    int value()    { return 5; } 
} 
namespace ns2  
{ 
    const double x = 100; 
    double value() {  return 2*x; } 
} 
  
int main() 
{ 
    // Access value function within ns1 
    cout << ns1::value() << '\n';  
  
    // Access value function within ns2 
    cout << ns2::value() << '\n';  
  
    // Access variable x directly 
    cout << ns2::x << '\n';        
  
    return 0; 
} 
// A C++ code to demonstrate that we can define  
// methods outside namespace. 
#include <iostream> 
using namespace std; 
  
// Creating a namespace 
namespace ns 
{ 
    void display(); 
    class geek 
    { 
    public: 
       void display(); 
    }; 
} 
  
// Defining methods of namespace 
void ns::geek::display() 
{ 
    cout << "ns::geek::display()\n"; 
} 
void ns::display() 
{ 
    cout << "ns::display()\n"; 
} 
  
// Driver code 
int main() 
{ 
    ns::geek obj; 
    ns::display(); 
    obj.display(); 
    return 0; 
} 
// A C++ program to demonstrate use of class 
// in a namespace 
#include <iostream> 
using namespace std; 
  
namespace ns 
{ 
    // A Class in a namespace 
    class geek 
    { 
    public: 
        void display() 
        { 
            cout << "ns::geek::display()\n"; 
        } 
    }; 
} 
  
int main() 
{ 
    // Creating Object of geek Class 
    ns::geek obj; 
  
    obj.display(); 
  
    return 0; 
} 

Source

Also in C++: