What is a namespace?
- Namespaces come in handy to avoid name conflicts when multiple programmers are involved.
- Any name in the namespace could be accessed using the scope resolution operator.
- To use the names without any qualifiers (::) keyword "using" is specified. e.g) "using namespace myspace1". All the names are visible in the current scope.
EXAMPLE: Demonstrate the usage of namespaces
#include <iostream> using namespace std; void func () { cout << "func() in global space" << endl; } namespace myspace1 { void func () { cout << "func() in myspace1" << endl; } } namespace myspace2 { void func () { cout << "func() in myspace2" << endl; } } namespace myspace3 { void func1 () { cout << "func1() in myspace3" << endl; } } using namespace myspace3; void main() { func(); myspace1::func(); myspace2::func(); func1(); }OUTPUT:
func() in global space func() in myspace1 func() in myspace2 func1() in myspace3
0 comments:
Post a Comment