Tuesday, October 4, 2016

Muhammad Akbar

What is a namespace in C++-C++ Tutorial

Why Namespace is used?

You will find below line in almost all C++ code. All of the programs written in this tutorial also contains following line after include directives:

using namespace std;
The goal of this line is to tell compiler that we are using std namespace in the program. But what does a namespace mean? Moreover, why namespaces are used in C++?
A namespace is a container for identifiers. These identifiers can be classes, functions, objects, variables, constants etc. There are two reasons for use of namespace.

Purpose of Namespace

The first purpose is that namespaces allow to structure program into logical units. It means that you can place some classes or functions that perform a common task into one namespace.
The second purpose can be described by the following example:
You have two friends with the same name – Bill. When you speak with somebody about Bill, you have to specify some additional information that which Bill you are talking about. It can be his address or first name. The same thing happens when you are using different libraries that have the same identifiers for different object. You have to tell to compiler additional information about which object is used. In this case, you have to use the object’s namespace.
A namespace is defined by using namespace keyword:

Namespace namespaceName
{
	//the content of namespace
}
This is an example of defining your own namespaces:

namespace myFirstNamespace
{
	void foo()
	{
		cout << "foo is called from myFirst namespace" << endl;
	}
}

namespace mySecondNamespace
{
	void foo()
	{
		cout << "foo from mySecondNamespace is called" << endl;
	}
}
As you can see, there are two functions foo() with the same name, but in different namespaces. If you want to call function foo(), you will have to specify the namespace of this function. It can be done by using the name of namespace with ::. For example,

//call function foo from myFirstNamespace
myFirstNamespace::foo();
//call function foo from mySecondNamespace
mySecondNamespace::foo();
Try to compile this code. The output should be:
foo from myFirstNamespace is called
foo from mySecondNamespace is called
There is a possibility to avoid appending namespace name every time we use an object from this namespace. You can use directive using. Look on the following line:

using namespace std;
This directive tells the compiler to use std namespace in the program. You can use your own namespaces. Try to add this using directive before main function:

using namespace myFirstNamespace;
And now, modify main function by removing appended namespace before calling function foo():

//call function foo from myFirstNamespace
foo();
//call function foo from myFirstNamespace too
foo();
In this case, the compiler will look for foo() only in myFirstNamespace:
foo from myFirstNamespace is called
foo from myFirstNamespace is called
using directive can be used only in the case when there is not an ambiguity between namespaces. Try do use both namespaces in the same time:

using namespace myFirstNamespace;
using namespace mySecondNamespace;
After this you can’t call foo() function without specifying its namespace, because both namespaces contain function foo(). If you try to do this, you will get an error:
more than one instance of overloaded function "foo" matches the argument list:
function "myFirstNamespace::foo()"
function "mySecondNamespace::foo()"

Same namespace in multiple places

A namespace can be defined in different parts of the program. All the objects that are in different parts of a program, but if in same namespace will form one namespace. For better understanding, look on example:

namespace myFirstNamespace
{
	void foo()
	{
		cout << "foo from myFirstNamespace is called" << endl;
	}
}
namespace mySecondNamespace
{
	void foo()
	{
		cout << "foo from mySecondNamespace is called" << endl;
	}
}
namespace myFirstNamespace
{
	int x = 10;
}
int main()
{
	myFirstNamespace::foo();
	cout << "X = " << myFirstNamespace::x << endl;
	cout << "Y = " << myFirstNamespace::y << endl;
	return 0;
}
namespace myFirstNamespace
{
	double y = 3.14;
}
myFirstNamespace consists of three elements: void foo(), int x and double y. When we write:

namespace some_namespace
{
}
When we create a new namespace named "some_namespace" but if in case, "some_namespace" already exists then new objects will be appended to the existing namespace.

Nested Namespace

You can create nested namespaces. One namespace can be a part of another namespace. For example, we can create namespace X, which will contain namespace Y:

namespace X
{
	void foo()
	{
		cout << "foo from X is called" << endl;
	}
	namespace Y
	{
		void foo()
		{
			cout << "foo from Y is called" << endl;
		}
	}
}
If you want to call function foo() from namespace Y, you will have to specify the full path to foo() function:

X::Y::foo();
Above line of code means that you are calling function foo from namespace Y, which is a part of namespace X.

Namespace alias

There is a possibility to use namespace alias. It can be useful when you have a very long namespace name or there are many nested namespaces. The use of alias has following syntax:
Namespace newName = oldName or NestedNamespace.
For example, we can create alias that will access Y namespace from the previous example:

namespace XY = X::Y;
Now you can call function foo() using new namespace name:

XY::foo();

Unnamed Namespace

You can declare an un-named namespace. An unnamed namespace is an alternative of using static global variables. Variables and function from an unnamed namespace will be visible in entire file. The syntax of declaring unnamed namespace is:

Namespace
{
	//Declaration list;
}
In the program, a unique identifier will be given to unnamed namespace. But you will not be able to know this identifier.

          

 Basic OOPS Concepts

Muhammad Akbar

About Muhammad Akbar -

Author Description here.. Nulla sagittis convallis. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.

Subscribe to this Blog via Email :

Hello Every One thanks For Visit My site You Can Suggest any Idea About site And Related Education Information Provide me Its Best Content For you am Include in this Site And feel free contact And email now I hope This site very help full you