Demonstrating namespaces. - C++ Statement

C++ examples for Statement:namespace

Description

Demonstrating namespaces.

Demo Code

#include <iostream> 
using namespace std; 

int integer1 = 98; // global variable 

namespace Example { 
   const double PI = 3.14159; 
   const double E = 2.71828; 
   int integer1 = 8; 

   void printValues(); // prototype 

   // nested namespace 
   namespace Inner 
   { /*from   w  ww .  j  a v a 2  s . co  m*/
       enum Years { FISCAL1 = 1990, FISCAL2 , FISCAL3 }; 
   }
}

namespace 
{ 
   double doubleInUnnamed = 88.22; // declare variable 
}
int main() 
{ 
   // output value doubleInUnnamed of unnamed namespace 
   cout << "doubleInUnnamed = " << doubleInUnnamed; 

   // output global variable 
   cout << "\n(global) integer1 = " << integer1; 

   // output values of Example namespace 
   cout << "\nPI = " << Example::PI << "\nE = " << Example::E 
       << "\ninteger1 = " << Example::integer1 << "\nFISCAL3 = " 
       << Example::Inner::FISCAL3 << endl; 

   Example::printValues(); // invoke printValues function 
}

void Example::printValues() 
{ 
   cout << "\nIn printValues:\ninteger1 = " << integer1 << "\nPI = " 
       << PI << "\nE = " << E << "\ndoubleInUnnamed = " 
       << doubleInUnnamed << "\n(global) integer1 = " << ::integer1 
       << "\nFISCAL3 = " << Inner::FISCAL3 << endl; 
}

Result


Related Tutorials