SlideShare a Scribd company logo
5
Most read
7
Most read
14
Most read
Namespace in C++
Presented By:-
Himanshu Choudhary
(15STUJPCS0004)
Contents
Introduction
Namespace fundamentals
Namespace features
 Nested namespace
 using namespace
 Global namespace
 Standard Library std namespace
 namespaces are open
Namespace v/s class
Introduction
If a program uses classes and functions written by
different programmers, it may be that the same
name is used for different things
Namespaces help us deal with this problem
Namespace Fundamentals
• A namespace is a declarative region that provides a scope to the
identifiers (the names of types, functions, variables, etc) inside it.
• It is used to organize code into logical groups and to prevent name
collisions that can occur especially when our code base includes
multiple libraries
• Namespace provides a class-like modularization without class-like
semantics
• Obliviates the use of File Level Scoping of C (file )static
Syntax
namespace Name_Space_Name
{
Some_Code
int x=4;
}
To print value of x in
main()
cout<<Name_Space_
Name::x;
#include <iostream>
using namespace std;
namespace MyNameSpace
{
int myData; // Variable in namespace
void myFunction()
{
cout << "MyNameSpace myFunction" << endl; }
// Function in namespace
class MyClass { int data; // Class in namespace
public:
MyClass(int d) : data(d) { }
void display() { cout << "MyClass data = " << data}
};
}
int main()
{
MyNameSpace::myData = 10;
// Variable name qualified by
namespace name
cout << "MyNameSpace::myData = " <<
MyNameSpace::myData << endl;
MyNameSpace::myFunction();
// Function name qualified by
namespace name
MyNameSpace::MyClass obj(25);
// Class name qualified by
namespace name
obj.display();
return 0;
}
Example
Namespace Features
• Nested namespace
• using namespace
• Global namespace
• Standard Library std namespace
• Namespaces are open
Nested Namespace
#include <iostream>
using namespace std;
int data = 0; // Global
namespace name1
{
int data = 1; // In namespace name1
namespace name2
{
int data = 2; // In nested namespace
} name1::name2
}
int main() {
cout << data << endl; // 0
cout << name1::data << endl; // 1
cout << name1::name2::data; // 2
cout<< endl;
return 0;
}
Slide 12- 9
“using namespace” keyword
A using declaration (using std::cout;) makes
only one name available from the namespace
A using directive makes all the names in the
namespace available
A using directive potentially introduces a name
If ns1 and ns2 both define my_function,
using namespace ns1;
using namespace ns2;
is OK, provided my_function is never used!
 Using using namespace we can avoid lengthy prexes
#include <iostream>
using namespace std;
namespace name1 {
int v11 = 1;
int v12 = 2;
}
namespace name2 {
int v21 = 3;
int v22 = 4;
}
using namespace name1; // All symbols of namespace
name1 will be available
using name2::v21; // Only v21 symbol of namespace
name2 will be available
int main()
{
cout << v11 << endl; // name1::v11
cout << name1::v12 << endl; // name1::v12
cout << v21 << endl; // name2::v21
cout << name2::v21 << endl; // name2::v21
cout << v22 << endl; // Treated as undefined
return 0;
}
Using “using” keyword
#include <iostream>
using namespace std;
int data = 0; // Global Data
namespace name1
{ int data = 1; // namespace Data }
int main()
{
using name1::data;
cout << data << endl; // 1 // name1::data -- Hides global data
cout << name1::data << endl; // 1
cout << ::data << endl; // 0 // ::data -- global data
return 0; }
Global Namespace
 Items in Global namespace
may be accessed by scope
resolution operator (::)
 Entire C++ Standard Library is put in its own namespace, called std
Standard library std Namespace
Without using using std With using using std
#include <iostream>
int main()
{
int num;
std::cout << "Enter a value: " ;
std::cin >> num;
std::cout << "value is: " ;
std::cout << num ;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter a value: " ;
cin >> num;
cout << "value is: " ;
cout << num ;
return 0;
}
 namespace are open: New Declarations can be added
#include <iostream>
using namespace std;
namespace cs
{ int x = 30; }
namespace cs
{ int y = 40; }
int main() {
using namespace cs;
x = y = 20;
cout << x << " " << y ;
return 0 ;
}
Output: 20 20
Namespaces are open
Namespace Class
Every namespace is not a class Every class defines a namespace
A namespace can be reopened and more
declaration can be added to it
A class cannot be reopened
No instance of a namespace can be created A class has multiple instances
using-declarations can be used to short-cut
namespace qualication
No using-like declaration for a class
A namespace may be unnamed An unnamed class is not allowed
Namespace v/s Class
Namespace in C++ Programming Language

More Related Content

PPTX
Encapsulation
PPTX
Friend function
PPT
Thread model in java
PPTX
Templates in c++
PPTX
C Programming: Structure and Union
PDF
Network security - OSI Security Architecture
PPT
Bootstrap Part - 1
PPTX
Pointers in c++
Encapsulation
Friend function
Thread model in java
Templates in c++
C Programming: Structure and Union
Network security - OSI Security Architecture
Bootstrap Part - 1
Pointers in c++

What's hot (20)

PPTX
Member Function in C++
PPTX
PPTX
Operators in java
PPTX
classes and objects in C++
PPTX
Variables in python
PPTX
[OOP - Lec 19] Static Member Functions
PPTX
Methods in java
PPT
Operator Overloading
PPTX
PDF
Methods in Java
PPTX
Tokens expressionsin C++
PDF
Operator overloading
PDF
PPTX
Constructor in java
PPTX
Function C programming
PPTX
Classes, objects in JAVA
PPTX
Functions in c
PDF
Constructors and Destructors
PPTX
Java abstract class & abstract methods
PPTX
Strings in c++
Member Function in C++
Operators in java
classes and objects in C++
Variables in python
[OOP - Lec 19] Static Member Functions
Methods in java
Operator Overloading
Methods in Java
Tokens expressionsin C++
Operator overloading
Constructor in java
Function C programming
Classes, objects in JAVA
Functions in c
Constructors and Destructors
Java abstract class & abstract methods
Strings in c++
Ad

Similar to Namespace in C++ Programming Language (20)

PPTX
Namespace1
PDF
Namespace--defining same identifiers again
DOCX
18 dec pointers and scope resolution operator
DOCX
PPTX
Object Oriented Programming Using C++: C++ Namespaces.pptx
DOCX
Programming in c plus plus4
PPT
Namespace
PDF
OOP_EXPLAINED_example_of_cod_and_explainations.pdf
PPTX
ECMAScript 2015
PPTX
Briefly Rust
PPTX
iOS Session-2
PPTX
C++ basics
PDF
C++primer
PDF
Object Oriented Programming (OOP) using C++ - Lecture 5
PDF
Perl tutorial final
PDF
Write a C++ program 1. Study the function process_text() in file.pdf
PPTX
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
PPTX
Rust vs C++
PDF
Addressing Scenario
PPTX
Twitter Author Prediction from Tweets using Bayesian Network
Namespace1
Namespace--defining same identifiers again
18 dec pointers and scope resolution operator
Object Oriented Programming Using C++: C++ Namespaces.pptx
Programming in c plus plus4
Namespace
OOP_EXPLAINED_example_of_cod_and_explainations.pdf
ECMAScript 2015
Briefly Rust
iOS Session-2
C++ basics
C++primer
Object Oriented Programming (OOP) using C++ - Lecture 5
Perl tutorial final
Write a C++ program 1. Study the function process_text() in file.pdf
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
Rust vs C++
Addressing Scenario
Twitter Author Prediction from Tweets using Bayesian Network
Ad

Recently uploaded (20)

PPTX
UNIT 4 Total Quality Management .pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
additive manufacturing of ss316l using mig welding
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Welding lecture in detail for understanding
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Geodesy 1.pptx...............................................
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
composite construction of structures.pdf
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
UNIT 4 Total Quality Management .pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Embodied AI: Ushering in the Next Era of Intelligent Systems
additive manufacturing of ss316l using mig welding
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
CYBER-CRIMES AND SECURITY A guide to understanding
Welding lecture in detail for understanding
Lesson 3_Tessellation.pptx finite Mathematics
Operating System & Kernel Study Guide-1 - converted.pdf
Geodesy 1.pptx...............................................
OOP with Java - Java Introduction (Basics)
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
composite construction of structures.pdf
Arduino robotics embedded978-1-4302-3184-4.pdf
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Mechanical Engineering MATERIALS Selection
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk

Namespace in C++ Programming Language

  • 1. Namespace in C++ Presented By:- Himanshu Choudhary (15STUJPCS0004)
  • 2. Contents Introduction Namespace fundamentals Namespace features  Nested namespace  using namespace  Global namespace  Standard Library std namespace  namespaces are open Namespace v/s class
  • 3. Introduction If a program uses classes and functions written by different programmers, it may be that the same name is used for different things Namespaces help us deal with this problem
  • 4. Namespace Fundamentals • A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. • It is used to organize code into logical groups and to prevent name collisions that can occur especially when our code base includes multiple libraries • Namespace provides a class-like modularization without class-like semantics • Obliviates the use of File Level Scoping of C (file )static
  • 5. Syntax namespace Name_Space_Name { Some_Code int x=4; } To print value of x in main() cout<<Name_Space_ Name::x;
  • 6. #include <iostream> using namespace std; namespace MyNameSpace { int myData; // Variable in namespace void myFunction() { cout << "MyNameSpace myFunction" << endl; } // Function in namespace class MyClass { int data; // Class in namespace public: MyClass(int d) : data(d) { } void display() { cout << "MyClass data = " << data} }; } int main() { MyNameSpace::myData = 10; // Variable name qualified by namespace name cout << "MyNameSpace::myData = " << MyNameSpace::myData << endl; MyNameSpace::myFunction(); // Function name qualified by namespace name MyNameSpace::MyClass obj(25); // Class name qualified by namespace name obj.display(); return 0; } Example
  • 7. Namespace Features • Nested namespace • using namespace • Global namespace • Standard Library std namespace • Namespaces are open
  • 8. Nested Namespace #include <iostream> using namespace std; int data = 0; // Global namespace name1 { int data = 1; // In namespace name1 namespace name2 { int data = 2; // In nested namespace } name1::name2 } int main() { cout << data << endl; // 0 cout << name1::data << endl; // 1 cout << name1::name2::data; // 2 cout<< endl; return 0; }
  • 9. Slide 12- 9 “using namespace” keyword A using declaration (using std::cout;) makes only one name available from the namespace A using directive makes all the names in the namespace available A using directive potentially introduces a name If ns1 and ns2 both define my_function, using namespace ns1; using namespace ns2; is OK, provided my_function is never used!
  • 10.  Using using namespace we can avoid lengthy prexes #include <iostream> using namespace std; namespace name1 { int v11 = 1; int v12 = 2; } namespace name2 { int v21 = 3; int v22 = 4; } using namespace name1; // All symbols of namespace name1 will be available using name2::v21; // Only v21 symbol of namespace name2 will be available int main() { cout << v11 << endl; // name1::v11 cout << name1::v12 << endl; // name1::v12 cout << v21 << endl; // name2::v21 cout << name2::v21 << endl; // name2::v21 cout << v22 << endl; // Treated as undefined return 0; } Using “using” keyword
  • 11. #include <iostream> using namespace std; int data = 0; // Global Data namespace name1 { int data = 1; // namespace Data } int main() { using name1::data; cout << data << endl; // 1 // name1::data -- Hides global data cout << name1::data << endl; // 1 cout << ::data << endl; // 0 // ::data -- global data return 0; } Global Namespace  Items in Global namespace may be accessed by scope resolution operator (::)
  • 12.  Entire C++ Standard Library is put in its own namespace, called std Standard library std Namespace Without using using std With using using std #include <iostream> int main() { int num; std::cout << "Enter a value: " ; std::cin >> num; std::cout << "value is: " ; std::cout << num ; return 0; } #include <iostream> using namespace std; int main() { int num; cout << "Enter a value: " ; cin >> num; cout << "value is: " ; cout << num ; return 0; }
  • 13.  namespace are open: New Declarations can be added #include <iostream> using namespace std; namespace cs { int x = 30; } namespace cs { int y = 40; } int main() { using namespace cs; x = y = 20; cout << x << " " << y ; return 0 ; } Output: 20 20 Namespaces are open
  • 14. Namespace Class Every namespace is not a class Every class defines a namespace A namespace can be reopened and more declaration can be added to it A class cannot be reopened No instance of a namespace can be created A class has multiple instances using-declarations can be used to short-cut namespace qualication No using-like declaration for a class A namespace may be unnamed An unnamed class is not allowed Namespace v/s Class