actually most of us when beginning to write a C++ code for example we write
using namespace std ;
but most of us doesn’t recognize what does that mean and what’s the Namespace so this topic basically will talk about namespaces
what’s the NAMESPACE :
it’s and entity that can group inside it a number of classes , object and functions . it’s name is divided into two section ” name ” , ” space ” so actually it’s a space designed for the name of the variable , to prevent the collision of names , suppose you have a function called connect () and you want to make a newer version of it inside the same File , and you don’t want to go through alot of naming Connect_new , Connect_newer …etc so we assign a namespace for each one
to access elements inside the name space we need to use the operator ” :: ” ;
for example
namespace mynamespace { int x , y ; } void main () { mynamespace::x ; mynamespace::y ; }
multiple name spaces in the same file :
more things that make naming collision :
if you are including two header files, those header files have a common function naming inside it which function will get executed when called ? acutally nothing will get executed and and error appears calling
error C2084: function 'connect()' already has a body
the using operator :
using operator is a keyword that introduces a certain namespace to a specific region so when you write
using namespace mynamespace ;
you can call connect function without using the operator ::
mynamespace::connect () ; using namespace mynamespace ; connect() ; // it should work now =)
Namespace STD :
all standard libraries inside C++ standard library lies in the name space std , so we always write the name space std to call the attributes at once instead of writing
std::cout<<"hellow world " ; std::cin>> x ; using namespace std ; cout <<"now no need to use std:: " ;
that’s it , this is acutally every thing about the explanation of ” using namespace std ; ” for any further questions don’t hesitate to ask here or DM me i always check my DMs @hadyelsahar
Leave a Reply