Tuesday 22 August 2017

What is namespace in C++?

when you use the namespace, your program looks like:-

#include<iostream>
using namespace std;
int main()
{
 cout<<"Hello World";
 return 0;
}
when you don't use the namespace, your program looks like:-

#include<iostream>
int main()
{
 std::cout<<"Hello World";
 return 0;
}
without "using namespace std;" when you write for example "cout<<;", you'd have to put "std::cout<<;",
Another role of the namespace is that it solves name conflicts among files and allow them to use with the same name.Let take an example,
you have a file let say main, and you want to include two files file_one and file_two in main but file_one and file_two both have the same function display when you include these file in the main file and call these file having the same function name it produces a conflict and shows an error.
The error will be the redefinition of function this is because we are not declaring display inside any declarative region such as namespace.C++ think you are redefining the function.To solve this conflicts we use the namespace.we use namespace then file name then we define the function in both file then we use this function in the main file in INT MAIN() { file_one::function}. so namespace avoids collision among same name file and makes them executable.




0 comments:

Post a Comment