What is a namespace? How do you resolve the name conflicts using namespaces? Explain with an example.
Namespace
•
Namespaces are used to group the entities like class, variables, objects,
function under a name.
•
The namespaces help to divide global
scope into sub‒scopes, where each sub‒scope has its own name.
#include <iostream>
using namespace std;
namespace ns1
{
int a = 5;
}
namespace ns2
{
char a[] =
"Hello";
}
int main()
{
cout<<ns1::a
<< endl;
cout<<ns2::a
<< endl;
return 0;
}
Output
5
Hello
Program Explanation:
In
above program there are two different namespaces containing the variable a. The variable a in the first namespace ns1
is of type int but the variable a in the second namespace ns2 is of array of characters. The both
the entities are treated separately. There is no re‒declaration error for
variable a.
Keyword using
•
The keyword using is used to introduce the namespace being used currently. For
instance we can have
using
namespace ns2
•
All the files in the C++ standard library declare all of its entities within
the std namespace. That is why in
the C++ program the std namespace is declared as follows ‒
using namespace
std;
•
This statement is used in the program
which uses any entity declared in
iostream.
•
Following are the rules of namespaces –
1)
The namespace must be defined using the keyword namespace.
2)
The namespace definition must appear at the global scope, or it can be present
inside another namespace(i.e. nested)
3)
The definition of namespace must not be terminated with semicolon.
4)
It is not allowed to create an instance of namespace.
5)
There can be unnamed namespace as well.
6)
The namespace definition is valid over multiple files. There is no need to
redefine it across multiple files.
7)
The namespace declaration do not have access specifiers (public or private)
1. What is a namespace? How do you resolve the name conflicts using namespaces? Explain with an example.
Object Oriented Programming: Chapter 1: Principles of Object Oriented Programming : Tag: Oops, Computer Programming : C++ Programming - Namespace
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Applied Physics (CSIE) II
PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Digital Principles and Computer Organization
CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electrical and Electronics Engineering
EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs) - Laboratory
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation