C++/Language/NameSpace

Материал из C\C++ эксперт
Версия от 10:28, 25 мая 2010; Admin (обсуждение | вклад) (1 версия: Импорт контента...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

A namespace can be nested within another

 
#include <iostream>
using namespace std;
namespace MyNameSpace1 {
  int i;
  namespace MyNameSpace2 { // a nested namespace
    int j;
  }
}
int main()
{
  MyNameSpace1::i = 19;
  MyNameSpace1::MyNameSpace2::j = 10;
  cout << MyNameSpace1::i << " "<<  MyNameSpace1::MyNameSpace2::j << "\n";
  using namespace MyNameSpace1;
  cout << i * MyNameSpace2::j;
  return 0;
}


Defines and tests namespaces.

 
#include <string>       
                        
namespace MySpace
{
   std::string mess = "Within MySpace";
   int count = 0;       
   double f( double);   
}
namespace YourSpace
{
   std::string mess = "Within YourSpace";
   void f( ) {                    
      mess += "!";
   }
}
namespace MySpace               
{
   int g(void);            
   double f( double y)     
   {                       
     return y / 10.0;
   }
}
int MySpace::g( )             
{                             
   return ++count;
}
#include <iostream>   
int main()
{
  std::cout<< MySpace::mess << std::endl;
  MySpace::g();
  std::cout << "\nReturn value g(): " << MySpace::g()
            << "\nReturn value f(): " << MySpace::f(1.2)
            << std::endl;
  YourSpace::f();
  std::cout << YourSpace::mess << std::endl;
  return 0;
}


Demonstrate a namespace

 
#include <iostream>
using namespace std;
namespace MyNameSpace {
  int upperbound;
  int lowerbound;
  class counter {
     int count;
   public:
     counter(int n) {
       if(n <= upperbound) 
          count = n;
       else 
          count = upperbound;
     }
     void reset(int n) {
       if(n <= upperbound) 
          count = n;
     }
     int run() {
       if(count > lowerbound) 
          return count--;
       else 
          return lowerbound;
     }
  };
}
int main()
{
  MyNameSpace::upperbound = 100;
  MyNameSpace::lowerbound = 0;
  MyNameSpace::counter ob1(10);
  int i;
  do {
    i = ob1.run();
    cout << i << " ";
  } while(i > MyNameSpace::lowerbound);
  cout << endl;
  MyNameSpace::counter ob2(20);
  do {
    i = ob2.run();
    cout << i << " ";
  } while(i > MyNameSpace::lowerbound);
  cout << endl;
  ob2.reset(100);
  MyNameSpace::lowerbound = 90;
  do {
    i = ob2.run();
    cout << i << " ";
  } while(i > MyNameSpace::lowerbound);
  return 0;
}


Enclosure variables with namespace

  
#include <iostream>
using namespace std;
namespace first
{
  int x = 5;
  int y = 10;
}
namespace second
{
  double x = 3.1416;
  double y = 2.7183;
}
int main () {
  using first::x;
  using second::y;
  cout << x << endl;
  cout << y << endl;
  cout << first::y << endl;
  cout << second::x << endl;
  return 0;
}


Namespace code section

  
#include <iostream>
using namespace std;
namespace first
{
  int x = 5;
}
namespace second
{
  double x = 3.1416;
}
int main () {
  {
    using namespace first;
    cout << x << endl;
  }
  {
    using namespace second;
    cout << x << endl;
  }
  return 0;
}


Namespace Demo: define a namespace

 
#include <iostream>
using namespace std;

namespace MyNameSpace {
  class demo {
    int i;
  public:
    demo(int x) { 
       i = x; 
    }
    void seti(int x) { 
       i = x; 
    }
    int geti() { 
       return i; 
    }
  };
  char str[] = "Illustrating namespaces\n";
  int counter;
}
namespace SecondNamespace {
  int x, y;
}
int main()
{
  MyNameSpace::demo ob(10);              // use scope resolution
  cout << "Value of ob is : " << ob.geti();
  cout << endl;
  ob.seti(99);
  cout << "Value of ob is now : " << ob.geti();
  cout << endl;

  using MyNameSpace::str;                // bring str into current scope
  cout << str;
  using namespace MyNameSpace;           // bring all of MyNameSpace into current scope
  for(counter = 10; counter; counter--)
    cout << counter << " ";
  cout << endl;
  SecondNamespace::x = 10;                  // use SecondNamespace namespace
  SecondNamespace::y = 20;
  cout << "x, y: " << SecondNamespace:: x;
  cout << ", " << SecondNamespace::y << endl;
  using namespace SecondNamespace;          // bring another namespace into view
  demo xob(x), yob(y);
  cout << "xob, yob: " << xob.geti() << ", ";
  cout << yob.geti() << endl;
  return 0;
}


Namespaces are additive

 

#include <iostream>
using namespace std;
namespace Demo {            // In Demo namespace
  int a; 
}
int x;                      // this is in global namespace
namespace Demo {
  int b;                    // this is in of Demo namespace, too
}
int main()
{
  using namespace Demo;
  a = b = x = 100;
  cout << a << " " << b << " " << x;
  return 0;
}


Some Namespace Options

 
#include <iostream>
using namespace std;
namespace MyNameSpace {
  int i;
}
namespace MyNameSpace {
  int j;
}
int main()
{
  MyNameSpace::i = MyNameSpace::j = 10;
  
  cout << MyNameSpace::i * MyNameSpace::j << "\n";   
  
  using namespace MyNameSpace;
  cout << i * j;
  return 0;
}


use explicit std:: qualification.

 
#include <iostream>
int main()
{
  double val;
  std::cout << "Enter a number: ";
  std::cin >> val;
  std::cout << "This is your number: ";
  std::cout << val;
  return 0;
}


Use namespaces to wrap variables

  
#include <iostream>
using namespace std;
namespace first
{
  int var = 5;
}
namespace second
{
  double var = 3.1416;
}
int main () {
  cout << first::var << endl;
  cout << second::var << endl;
  return 0;
}


Using namespace to reference variables

  
#include <iostream>
using namespace std;
namespace first
{
  int x = 5;
  int y = 10;
}
namespace second
{
  double x = 3.1416;
  double y = 2.7183;
}
int main () {
  using namespace first;
  cout << x << endl;
  cout << y << endl;
  cout << second::y << endl;
  cout << second::x << endl;
  return 0;
}