Monday, March 22, 2021

Using namespace std;

 Understanding "Using namespace std;

namespace is a declarative region that provide scope to identifiers and is used to avoid the name collision ensuring uniqueness of names.

Let's take real life example to understand namespace

suppose we have two friends
friend 1 ava
friend 2 ava

both have the same name. so the real question is how to call friend 1 and how to access friend one if both have the same name.
so we use some additional information about friend 1 and friend 2 to avoid the confusion . 
we can use something like the place they belong or address to access and call them. And this will help in overcoming the difficulties of name collision.

similarly in C++ there can be methods , functions, variables and others having same name and they can be included in some standard or user defined files . so whenever they are used computer will try to access the point to get the code  and include that in our program for successful compilation.
so this namespace tells the computer where to look for that particular method to be included in our program. 

SO WE CAN SAY THIS NAMESPACE HAVE THE ADDRESS OF ALL THE METHODS DEFINED IN DIFFERENT FILES THAT IS INCLUDED AND THIS NAMESPACE TELLS THE COMPUTER WHERE TO LOOKS FOR THAT METHOD TO AVOID CONFUSION.

why namespace std in C++ ?
we include a header file #include<iostream>
in our C++ program and inside main function
we use method like cout cin endl etc...
so iostream contains all these method to be
used in a program . so when it  is used computer 
will look for these method to include in 
our program. So if namespace std; is not used
and there maybe more than one header file so computer will
not find where to look for that exactly and it will through error.
and if we have used namespace std;
it will tell the computer to look for method like cout ,cin...
in iostream and it will work.

Understanding with pictures




       Author   
   Md wasif



Join the CodifiedYOUTH community to learn free skills and get free internships and lot more
Join on Whatsapp
Join on Linkedin
visit CodifiedYOUTH website for more detail

No comments:

Post a Comment

Using namespace std;

  Understanding "Using namespace std; namespace is a declarative region that provide scope to identifiers and is used to avoid the name...