Hello friends, in this post today, we will learn about namespace in c++ and how to use in our program. so let's start....
![]() |
namespace in c++ |
Namespace:-
• It defines a scope for the identifiers that are utilized in a program.
• It puts the names of its members during a distinct space in order that they don’t conflict with the names in others namespace or global namespace.
• A namespace definition are often continued and extended over multiple files, they're not redefined or overridden.
• Namespace definition doesn’t terminates with a semicolon like in class data type definition.
• Remember namespace is not a class, you cannot create instance of namespace.
• The namespace definition must be done at worldwide scope, or nested insides another namespace.
• Using keyword allows you to import a whole namespace into your program with a worldwide scope.
• It are often wont to import a namespace into another namespace or any program.
• For using identifiers defined in namespace using directive is employed as follows: using namespace std;
• std is that the namesace where ANSI C++ standard class libraries are defined.
• All ANSI C++ programs must include this directive that is std.
• This may bring all the identifiers defined in std to the present global scope.
The syntax of namespaces is:
namespace namespace_identifier
{
entities
}
Where namespace_identifier is any valid identifier and entities is that the collection of classes, objects and functions that are included within the namespace.
For example:
namespace First_Namespace
{
int a, b, c;
}
In this case, the variables a, b and c are normal variables declared within a namespace called First_Namespace.
In order to access these variables from outside the First_Namespace namespace we have to use the scope resolution operator (::). For example, to access the previous variables from outside First_Namespace we can write:
First_Namespace::a
First_Namespace::b
First_Namespace::c
The functionality of namespaces is particularly useful within the case that there's an opportunity that a worldwide object or function uses an equivalent identifier as another one, causing redefinition errors.
Example:
Output:-
I hope that whatever information I have given in this post today, you have liked it and you have understood it.so keep learning and wait for the next post that will help you to increase your knowledge with the something new information.
Thank you so much for reading. And take care about yourself and your family.
Comments
Post a Comment