SlideShare a Scribd company logo
Namespace in
C++
Conten
ts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_Spac
e_ 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;)only one name available from the
namespace
makes
in theA using directive makes all the
names 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
 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
int main()
{
cout << v11 <<
endl;
//
name1::v11cout << name1::v12 << endl; //
name1::v12 cout << v21 << endl; //
name2::v21cout << 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
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
Namespace1

More Related Content

PPTX
working with files
ODP
To Infinity & Beyond: Protocols & sequences in Node - Part 2
PDF
Åsted .Net (CSI .Net)
PDF
Becoming an Advanced Groovy Developer
PPTX
Why learn Internals?
PDF
Storm introduction
PDF
Intro KaKao MRTE (MySQL Realtime Traffic Emulator)
PPTX
Large scale nlp using python's nltk on azure
working with files
To Infinity & Beyond: Protocols & sequences in Node - Part 2
Åsted .Net (CSI .Net)
Becoming an Advanced Groovy Developer
Why learn Internals?
Storm introduction
Intro KaKao MRTE (MySQL Realtime Traffic Emulator)
Large scale nlp using python's nltk on azure

What's hot (20)

PDF
Natural Language Toolkit (NLTK), Basics
PPTX
Pipes and filters
DOCX
40 basic linux command
PPTX
Multithreaded programming
PPTX
#2 (UDP)
PDF
Java Week4(C) Notepad
PPTX
PenTest using Python By Purna Chander
PPTX
Python lec4
PPTX
Python lec5
PPT
More than UI
PDF
Netty: asynchronous data transfer
PDF
The Ring programming language version 1.5.3 book - Part 25 of 184
PDF
DConf 2016: Keynote by Walter Bright
PDF
The Ring programming language version 1.9 book - Part 48 of 210
PDF
Fast and cost effective geospatial analysis pipeline with AWS lambda
PDF
F Files - Learnings from 3 years of Neos Support
PDF
C++ course start
PPTX
Functions in python
PDF
The Ring programming language version 1.9 book - Part 20 of 210
Natural Language Toolkit (NLTK), Basics
Pipes and filters
40 basic linux command
Multithreaded programming
#2 (UDP)
Java Week4(C) Notepad
PenTest using Python By Purna Chander
Python lec4
Python lec5
More than UI
Netty: asynchronous data transfer
The Ring programming language version 1.5.3 book - Part 25 of 184
DConf 2016: Keynote by Walter Bright
The Ring programming language version 1.9 book - Part 48 of 210
Fast and cost effective geospatial analysis pipeline with AWS lambda
F Files - Learnings from 3 years of Neos Support
C++ course start
Functions in python
The Ring programming language version 1.9 book - Part 20 of 210
Ad

Similar to Namespace1 (20)

PPTX
Namespace in C++ Programming Language
PDF
Namespace--defining same identifiers again
DOCX
18 dec pointers and scope resolution operator
DOCX
PPT
Namespace
DOCX
Programming in c plus plus4
PPTX
Object Oriented Programming Using C++: C++ Namespaces.pptx
PDF
OOP_EXPLAINED_example_of_cod_and_explainations.pdf
PPTX
C++ basics
PPTX
Briefly Rust
PPTX
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
PPTX
iOS Session-2
PPT
PDF
C++primer
PPTX
Rust vs C++
PPTX
Namespaces
PPTX
ECMAScript 2015
PPTX
Next .NET and C#
PDF
Perl tutorial final
PPTX
Presentation c++
Namespace in C++ Programming Language
Namespace--defining same identifiers again
18 dec pointers and scope resolution operator
Namespace
Programming in c plus plus4
Object Oriented Programming Using C++: C++ Namespaces.pptx
OOP_EXPLAINED_example_of_cod_and_explainations.pdf
C++ basics
Briefly Rust
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
iOS Session-2
C++primer
Rust vs C++
Namespaces
ECMAScript 2015
Next .NET and C#
Perl tutorial final
Presentation c++
Ad

More from zindadili (19)

PPTX
Exception handling
PPT
Exception handling
PPTX
Templates2
PPTX
Templates1
PPTX
Virtual function
PPTX
Operator overloaing
PPTX
Operator overloading2
PPTX
Polymorphism
PPTX
Function overloading
PPTX
Aggregation
PPTX
Hierarchical inheritance
PPTX
Hybrid inheritance
PPTX
Multiple inheritance
PPTX
Abstraction1
PPTX
Abstraction
PPTX
Access specifier
PPTX
Inheritance
PPTX
Friend function
PPTX
Enum
Exception handling
Exception handling
Templates2
Templates1
Virtual function
Operator overloaing
Operator overloading2
Polymorphism
Function overloading
Aggregation
Hierarchical inheritance
Hybrid inheritance
Multiple inheritance
Abstraction1
Abstraction
Access specifier
Inheritance
Friend function
Enum

Recently uploaded (20)

PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Pre independence Education in Inndia.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
RMMM.pdf make it easy to upload and study
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
master seminar digital applications in india
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Cell Types and Its function , kingdom of life
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Insiders guide to clinical Medicine.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPH.pptx obstetrics and gynecology in nursing
Pre independence Education in Inndia.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Microbial diseases, their pathogenesis and prophylaxis
RMMM.pdf make it easy to upload and study
2.FourierTransform-ShortQuestionswithAnswers.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
01-Introduction-to-Information-Management.pdf
Cell Structure & Organelles in detailed.
Anesthesia in Laparoscopic Surgery in India
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
master seminar digital applications in india
O7-L3 Supply Chain Operations - ICLT Program
Microbial disease of the cardiovascular and lymphatic systems
Cell Types and Its function , kingdom of life

Namespace1

  • 2. Conten ts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_Spac e_ 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;)only one name available from the namespace makes in theA using directive makes all the names 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
  • 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 int main() { cout << v11 << endl; // name1::v11cout << name1::v12 << endl; // name1::v12 cout << v21 << endl; // name2::v21cout << 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 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