adsense


Thursday, 26 November 2015

C++ LANGUAGE USING DIRECTIVE

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



C++ LANGUAGE USING DIRECTIVE




 
The using directive 



You can also avoid prepending of namespaces with the 
using namespace directive. This directive tells the 
compiler that the subsequent code is making use of 
names in the specified namespace. The namespace is 
thus implied for the following code: 

#include <iostream> 
using namespace std; 
 
// first name space 
namespace first_space{ 
   void func(){ 
      cout << "Inside first_space" << endl; 
   } 
} 
// second name space 
namespace second_space{ 
   void func(){ 
      cout << "Inside second_space" << endl; 
   } 
} 
using namespace first_space; 
int main () 
{ 
  
   // This calls function from first name space. 
   func(); 
    
   return 0; 
} 

If we compile and run above code, this would 
produce the following result: 
Inside first_space 
The ‘using’ directive can also be used to refer to 
a particular item within a namespace. For example, 
if the only part of the std namespace that you intend 
to use is cout, you can refer to it as follows: 

using std::cout; 

Subsequent code can refer to cout without prepending 
the namespace, but other items in the std namespace 
will still need to be explicit as follows: 

#include <iostream> 
using std::cout; 
 
int main () 
{ 
  
   cout << "std::endl is used with std!" << std::endl; 
    
   return 0; 
} 
If we compile and run above code, this would produce 
the following result: 

std::endl is used with std! 

Names introduced in a using directive obey normal scope rules. 
The name is visible from the point of the using directive to the 
end of the scope in which the directive is found. Entities 
with the same name defined in an outer scope are hidden. 



No comments: