0% found this document useful (0 votes)
33 views17 pages

CH 14namespaces

Namespace mechanisms in C++ help avoid name clashes by logically grouping variables, classes, functions, and other elements. Namespaces allow the same name to be used for different purposes. To use elements from a namespace, they must be brought into the global namespace using either a using directive or declaration, or by specifying the fully qualified namespace and element name. Unnamed namespaces provide a way to define elements that are unique within a translation unit.

Uploaded by

Dhruv Rawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views17 pages

CH 14namespaces

Namespace mechanisms in C++ help avoid name clashes by logically grouping variables, classes, functions, and other elements. Namespaces allow the same name to be used for different purposes. To use elements from a namespace, they must be brought into the global namespace using either a using directive or declaration, or by specifying the fully qualified namespace and element name. Unnamed namespaces provide a way to define elements that are unique within a translation unit.

Uploaded by

Dhruv Rawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Namespaces

Introduction
• Real applications or programs consist of many source or library files.
These files can be used and maintained by more than one developer or
programmer.
• As it uses different library files in a program there might be the cases
where there are same function or variable names being used. Because
of this there arises the name conflict problem.
• Namespace mechanism in C++ overcomes the problem of name
clashes in the global scope. The namespace mechanism allows an
application to be partitioned into different packages or enclosure.
Each enclosure can define and operate within its own scope.
• For eg we are using Math library and some educational library in our
program. Math library has abs() function to calculate absolute value
and education library has abs() for counting absent students. Here the
name confliction problem occurs.
• Namespaces are used to solve such name conflict problems.
• - The namespaces declaration identifies and assigned a unique
name to a user declared namespace. Namespaces allow
distinguishing names declared in different files

• - To use C++ namespaces, there are two steps involved:

• 1. To uniquely identify a namespace with the keyword


namespace.
• 2. To access the elements of an identified namespace by applying
the using keyword. For example:
namespace NewOne
- General form of namespace is: {
int p;
namespace indentifier long q
{ namespace body } }
Global Namespace
• The global namespace is the enclosure where our program runs.
• It is the environment accessible to the program while running.
• Any function , variable or class, must be brought to the global
namespace to be accessible.
• When we use different libraries, then all the variables and
functions in libraries come to the global namespace. As there is no
discrimination there arises the problem of ambiguity.
• For this, there was a need for logical grouping and Namespaces
provides logical grouping of names.
• The namespace identify logical group of elements and there
elements could be variables, classes and objects of them and
non-member functions as well.
Scope Resolution operator :: and fully
qualified names

• Once the elements are logically grouped using namespace


we can access it using scope resolution operator.
Eg: namespace::element.
math::abs() and Academic::abs()
These are said to be fully qualified names where we use the
name of the namespace and element name to access.
Using keyword
• Using keyword is used when we do not want to use the fully
qualified names.
• 1) Using Declaration :
#include<iostream>
//using namespace std;
int main()
{
using std::cout;
cout<< “hi”;
}
• 2) Using Directive:
Using namespace std;
Defining namespace
// MyNamespace.cpp
#include<iostream>
Using namespace std;
namespace MyNamespace
{
int MyNumber;
}
int main()
{
// using MyNamespace::MyNumber //using declaration
// cin >>MyNumber; cout<<MyNumber;

// using namespace MyNamespace //using directive


// cin>>MyNumber; cout<<MyNumber;

cin>>MyNamespace::MyNumber; //fully qualified names


cout<<MyNamespace::MyNumber<<endl; }
Function and class in namespace
• We can define function and class inside a namespace similarly like
variables.
• Eg:
Namespace MyNamespace
{
Void display()
{
Cout<<“Hello”;
}
}
int main()
{
// we can also use using declarative and using directive here.
MyNamespace::display();
}
#include <iostream>
#include<string>
Using namespace std;
namespace MyNamespace
{
Class brother
{
String name;
Public:
brother(string brothername)
{
name = brothername;
}
}};
int main()
{
MyNamespace::brother Lara(“kevin”)
cout<< Lara;
}
Defining inside and outside
// Defining Function outside the namespace
namespace MyNamespace
{
void display();
}
void MyNamespace::display()
{
Cout << “Hello”;
}
int main()
{
using namespace MyNamespace;
display();

}
// Defining class member function outside the namespace
Namespace MyNamespace
{
class brother
{
string name;
public:
brother(string brothername);
};
}
MyNamespace::Brother::brother(string brothername)
{
MyNamespace::brother::Name = brothername;
}
Int main()
{
MyNamespace::brother Lara(“kevin);
cout<<Lara; }
Unnamed namespace
• Use the keyword namespace without identifier before the closing
brace. This can be superior alternative to the use of the global static
variable declaration.

• - Each identifier that is enclosed within an unnamed namespace is


unique within the translation unit in which the unnamed namespace
is defined.

• - The syntax:
• namespace { namespace_body }

• - Behaves as if it were replaced by:

• namespace unique { namespace_body}


• using namespace unique;
#include <iostream>
#include <string>
namespace
{
int Mynumber;
string Myname;
}

int main()
{
cout<< “Enter number”;
cin>> Mynumber;
cout<<Mynumber;
};
Nested Namespaces
• We can define a namespace within another namespace.
• To access the element deep inside the nested namespace
we have to use the qualification using the syntax :

outerNamespace :: innerNamespace::elementName
Namespace Aliases
• When we have to give the long qualifications to access the namespace
element, we can use aliases to shorten them to access.
Namespace OuterNamespace
{
namespace InnerNamespace
{
class brother
{
string name;
public:
brother (stirng brothername)
{ name = brothername; }
};
}
}
• int main()
{
namespace NS = OuterNamespace::InnerNamespace;

NS::brother Lara(“kevin”);
Cout<< Lara;
}
Koenig Lookup
• Koenig lookup enables the compiler to find out namespace for
an element even if it is not mentioned with either using
directive or declarative or even fully qualified.
• The formal name of the algorithm is Augment dependent
lookup;

• The current namespace and the global namespace are always


searched
• – For each argument, additional namespaces are searched
• – All candidate namespaces are collated
• – Function name is looked up in the collated list
• – Overloads and conflict resolution proceed as normally

You might also like