What does 'using namespace std' mean in C++?
Suppose In u r class, if there are two persons with same name
vinod, and if u...
{
int a, b;
}
In this case, the variables a and b are normal variables declared
within a namespace called myname.
These va...
second_space::func(); // Calls function from second name space
}
output :
Inside first_space
Inside second_space
The conce...
Generally most of them will use the standard namespace with the following
declaration :
using namespace std;
 We can acce...
 Namespaces can be split :
◦ Two segments of a code can be declared in the same namespace i.e.
We can have more than one ...
Take a look with an example :
#include<iostream>
using namespace std;
namespace
{
int local;
}
int main()
{
local = 1;
cou...
#include"h1.h"
using namespace std;
void func();
namespace
{
int local=1;
}
int main()
{
local =3;
local1=4;
cout << "Loca...
#include<iostream>
using namespace std; // this namespace is for endl and cout
namespace m
{
int local=1;
}
namespace s=m;...
 with “ Using” declaration
1. SCOPE RESOLUTION :
From the name itself u have got some idea ..
Any name (identifier) decla...
2.WITH “USING” DECLARATION :
The keyword USING introduces a name into the current declarative
region (such as a block) . W...
cout << x << 'n';
cout << y << 'n';
cout << first::y << 'n';
cout << second::x << 'n';
return 0;
}
 Suppose if u put usin...
With using keyword u can import only one variable at a
time.But with using derivative , allows you to import an entire nam...
using and using namespace have validity only in the
same block in which they are stated or in the entire source code file ...
{
cout<<"this is fun 1F";
}
void g()
{
cout<<"this is fun 1G";
}
}
namespace second
{
void f()
{
cout<<"this is fun 2F";
}...
}
One can access members of nested namespace by using scope operators as
follows:
using namespace name1::name2; // to acce...
1. Programs those work in C but not in C++
2. Difference between malloc(),calloc(),realloc()
3. Dynamic memory allocation ...
of 16

Namespace--defining same identifiers again

It tells about the identifiers declaring again with namespaces at different blocks in the program
Published on: Mar 3, 2016
Published in: Education      
Source: www.slideshare.net


Transcripts - Namespace--defining same identifiers again

  • 1. What does 'using namespace std' mean in C++? Suppose In u r class, if there are two persons with same name vinod, and if u want to call one person, how will u call him ?? Whenever we need to differentiate them (in case of more members ) definitely we would have to use some additional information along with their name, like surname or the area (if they live in different area) or their mother or father name, etc. Same situation arises here. Suppose in u r code , If u had declare the function with name say abc(), and there is some other library available with the same function name and if u include that library in u r code , Then the compiler has no way of thinking which xyz() function u r referring in the code. In order to overcome this difficulty Namespace is introduced. Namespace: Namespace is a container for a set of identifiers (names of variables, functions , classes) . It puts the names of its members in a distinct space so that they don't conflict with the names in other namespaces or global namespace. For example : 1. namespace myname Also See more posts : www.comsciguide.blogspot.com
  • 2. { int a, b; } In this case, the variables a and b are normal variables declared within a namespace called myname. These variables can be accessed from within their namespace normally, with their identifier (either a or b), but if accessed from outside the myname namespace they have to be properly qualified with the scope operator ::. For example, to access the previous variables from outside myname they should be qualified like: myname::a myname::b Example 2. #include <iostream> using namespace std; namespace first_space // first name space { void func() { cout << "Inside first_space" << endl; } } namespace second_space // second name space { void func() { cout << "Inside second_space" << endl; } } int main () { first_space::func(); // Calls function from first name space. Also See more posts : www.comsciguide.blogspot.com
  • 3. second_space::func(); // Calls function from second name space } output : Inside first_space Inside second_space The concept can be depicted using the following diagram: Also See more posts : www.comsciguide.blogspot.com
  • 4. Generally most of them will use the standard namespace with the following declaration : using namespace std;  We can access functions and variables with the scope operator in the same way. #include <iostream> using namespace std; namespace me { int value() { return 5; } } namespace me1 { const double pi = 3.1416; double value() { return 2*pi; } } int main () { cout << me::value() << 'n'; cout << me1::value() << 'n'; cout << me1::pi << 'n'; } Also See more posts : www.comsciguide.blogspot.com
  • 5.  Namespaces can be split : ◦ Two segments of a code can be declared in the same namespace i.e. We can have more than one namespace of the same name. This gives the advantage of defining the same namespace in more than one file (although they can be created in the same file as well). #include<iostream> using namespace std; namespace mynamespace { int x; } namespace mynamespace { int y; } It tells that in the same namespace two variables are declared seperately. Ofcouse u might think why declaring seperately in same file, this will help a lot while declaring the same namespace in different files.  We can have anonymous namespaces (namespace with no name). They are directly usable in the same program and are used for declaring unique identifiers. Also See more posts : www.comsciguide.blogspot.com
  • 6. Take a look with an example : #include<iostream> using namespace std; namespace { int local; } int main() { local = 1; cout << "Local=" << local << endl; return 0; } Here , as there is no name for namespace , we won't be using the scope operator It also avoids making global static variable. Take a look with an example : // this is h1.h file and u need to save it as “.h “ namespace { int local1; // as it is anonymous, the namespace is visible only upto this file } void func() { local1 = 2; } // this is main file #include<iostream> Also See more posts : www.comsciguide.blogspot.com
  • 7. #include"h1.h" using namespace std; void func(); namespace { int local=1; } int main() { local =3; local1=4; cout << "Local=" << local << endl; func(); cout << "Local1=" << local1 << endl; return 0; }  Namespace aliasing : It is also possible to declare an alternate name for an existing namespace. We can use the following format: namespace new_name = current_name; For example: Also See more posts : www.comsciguide.blogspot.com
  • 8. #include<iostream> using namespace std; // this namespace is for endl and cout namespace m { int local=1; } namespace s=m; // creating alternate name for namespace int main() { cout << "Local=" << s::local << endl; cout << "Local=" << m::local << endl; return 0; } Output: 1 1  C++ has a default namespace named std, which contains all the default library of the C++. USING A NAMESPACE There are 3 ways to use a namespace in program.  Scope resolution  with “ Using” directive Also See more posts : www.comsciguide.blogspot.com
  • 9.  with “ Using” declaration 1. SCOPE RESOLUTION : From the name itself u have got some idea .. Any name (identifier) declared in a namespace can be explicitly specified using the namespace's name and the scope resolution :: operator with the identifier. Take a look with an example : #include<iostream> using namespace std; namespace m { int local = 1; } namespace n { char a='A'; } int main() { int local=2; cout << local << endl; cout << m::local << endl; // scope resolution cout << n::a << endl; return 0; } Also See more posts : www.comsciguide.blogspot.com
  • 10. 2.WITH “USING” DECLARATION : The keyword USING introduces a name into the current declarative region (such as a block) . With this using declaration we can import a specific name in that block . Take a look with an example : #include <iostream> using namespace std; namespace first { int x = 5; int y = 10; } namespace second { double x = 3.1416; double y = 2.7183; } int main () { using first::x; // using declaration using second::y; // using declaration Also See more posts : www.comsciguide.blogspot.com
  • 11. cout << x << 'n'; cout << y << 'n'; cout << first::y << 'n'; cout << second::x << 'n'; return 0; }  Suppose if u put using declaration in a block, then it will be visible only in that block.  U can access the namespace from other files also. Try yourself this one and check the output. int main () { { using first::x; } using second::y; cout << x << 'n'; cout << y << 'n'; return 0; } 3.WITH USING DIRECTIVE : Also See more posts : www.comsciguide.blogspot.com
  • 12. With using keyword u can import only one variable at a time.But with using derivative , allows you to import an entire namespace into the program with global scope . It can be used to import a namespace into another namespace or any program. #include <iostream> using namespace std; namespace first { int x = 5; int y = 10; } namespace second { double x = 3.1416; double y = 2.7183; } int main () { using namespace first; // using directive cout << x << 'n'; cout << y << 'n'; cout << second::x << 'n'; cout << second::y << 'n'; return 0; } Also See more posts : www.comsciguide.blogspot.com
  • 13. using and using namespace have validity only in the same block in which they are stated or in the entire source code file if they are used directly in the global scope. For example, it would be possible to first use the objects of one namespace and then those of another one by splitting the code in different blocks. #include <iostream> using namespace std; namespace first { int x = 5; } namespace second { double x = 3.1416; } int main () { { // using directive in 1st block using namespace first; cout << x << 'n'; } { // using directive in 2nd block using namespace second; cout << x << 'n'; } return 0; } Name imported with using declaration can override the name imported with using derivative #include <iostream> using namespace std; namespace first { void f() Also See more posts : www.comsciguide.blogspot.com
  • 14. { cout<<"this is fun 1F"; } void g() { cout<<"this is fun 1G"; } } namespace second { void f() { cout<<"this is fun 2F"; } void g() { cout<<"this is fun 2G"; } } int main () { using namespace first; // using directive using second::f; // using declaration f(); // calls f() of second namespace return 0; } Nested Namespaces : Namespaces can be nested where you can define one namespace inside another name space as follows: namespace name1 { namespace name2 // namespace name2 is in the block of namespace name1 { fun(); } Also See more posts : www.comsciguide.blogspot.com
  • 15. } One can access members of nested namespace by using scope operators as follows: using namespace name1::name2; // to access members of name2 using namespace name1; // to access members of name1 Take a look with an example : #include <iostream> using namespace std; namespace name1 { void func() { cout << "Inside name1" << endl; } namespace name2 { void func() { cout << "Inside name2" << endl; } } } int main () { using namespace name1::name2; func(); // This calls function from name2 namespace. return 0; } The result of this program should be: ??? FOR REFERENCES : Also See more posts : www.comsciguide.blogspot.com
  • 16. 1. Programs those work in C but not in C++ 2. Difference between malloc(),calloc(),realloc() 3. Dynamic memory allocation (Stack vs Heap) 4. 3 ways to solve recurrence relations 5. Difference between Balanced,Ordered,Full,Strict,Complete,Perfect Binary tree 6. Graph Terminology 7. How analysis of algorithms is done? 8. EXTERN - wherever u define variables, it will get access to them 9. TYPEDEF - create variables by name 10. Comma - the lowest priority operator Also See more posts : www.comsciguide.blogspot.com