Namespaces OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani
using namespace std; We have been using the above statement in our programs. The using namespace statement specifies that the members defined in std namespace will be used frequently throughout the program.
Consider a situation, when we have two persons with the same name, Pooja, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like surname, area of living, father s name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.
A1.h class A //body of A ; A2.h class A //body of A ; Main_prog.cpp #include A1.h #include A2.h main() A Aobj; //ERROR: Ambiguity error due to multiple definitions of A
How to overcome such problems? A namespace is designed to overcome this difficulty and is used as additional information to differentiate similar functions, classes, variables etc. with the same name available in different libraries. In essence, a namespace defines a scope. Namespaces enable the C++ programmer to prevent pollution of the global namespace that leads to name clashes.
Defining a Namespace: namespace namespace_name //declarations of variables, functions, classes, etc. Difference between a class definition and namespace definition is, namespace is concluded with a closing brace but no terminating semicolon.
Ex: namespace TestSpace int m; void display(int n) cout<<n; //no semicolon here Here, the variable m and the function display() are inside the scope defined by TestSpace namespace. To assign value to m, use :: operator. TestSpace::m = 100; To call a function display() TestSpace::display(10); EX: NameSpace1.cpp
A1.h namespace A1 class A //body of A ; A2.h namespace A2 class A //body of A ; Main_prog.cpp #include A1.h #include A2.h main() A1::A Aobj1; // OK: Aobj1 is an object of the class defined in A1.h A2::A Aobj2; // OK: Aobj2 is an object of the class defined in A2.h
This approach becomes cumbersome if the members of a namespace are frequently used. In such cases, we can use using directive to simplify the access. 2 ways: using namespace namespace_name; //using directive using namespace_name :: member_name; //using declaration
Ex: using namespace TestSpace; m = 100; //OK display(200); //OK using TestSpace :: m; m = 100; //OK display(200); //NOT OK, display not visible Main_prog.cpp NOTE: the using directive brings back the #include A1.h #include A2.h global namespace pollution that the main() namespaces mechanism was supposed to using namespace A1; remove. A Aobj1; // OK: Aobj1 is an object of the class defined in A1.h using namespace A2; A Aobj2;
Some namespaces have long names. Qualifying the name of a class that is enclosed within such a namespace, with the name of the namespace, is cumbersome. Ex: namespace a_very_very_long_name class A - - - - - ; void main() a_very_very_long_name :: A A1;
Assigning a suitably short alias to such a long namespace solves the problem. namespace a_very_very_long_name class A ; - - - - - namespace x = a_very_very_long_name void main() x :: A A1;
Aliases provides benefits also. Suppose an alias has been used at a number of places in the source code. Ex: Suppose alias X refers to a namespace N1 namespace X = N1; //declaring an alias Suppose that this alias has been used extensively in the source code. X :: A obj; //obj is an object of class A that is enclosed in namespace N1 obj.f1(); //f1() is a member function of the above class If the declaration of alias X is modified as follows namespace X = N2; //modifying the alias, N2 is also a namespace Then all the references made by X would now refer to namespace N2.
Point to be noted: Such a statement would compile only if both namespaces, N1 and N2, contain a class named A, and if these two classes have the same interface.
References Sourav Sahay, Objected Oriented Programming with C++ E Balagurusamy, Objected Oriented Programming with C++