SlideShare a Scribd company logo
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
Ad

More Related Content

What's hot (20)

[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Operator overloading and type conversion in cpp
Operator overloading and type conversion in cppOperator overloading and type conversion in cpp
Operator overloading and type conversion in cpp
rajshreemuthiah
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Pranali Chaudhari
 
Inline function
Inline functionInline function
Inline function
Tech_MX
 
Abstraction java
Abstraction javaAbstraction java
Abstraction java
MahinImran
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
Exception handling
Exception handlingException handling
Exception handling
Pranali Chaudhari
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
Md. Ashraful Islam
 
Storage classes in c++
Storage classes in c++Storage classes in c++
Storage classes in c++
Jaspal Singh
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
ThamizhselviKrishnam
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vishal Patil
 
operator overloading & type conversion in cpp
operator overloading & type conversion in cppoperator overloading & type conversion in cpp
operator overloading & type conversion in cpp
gourav kottawar
 
OOP in C++
OOP in C++OOP in C++
OOP in C++
ppd1961
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
06. operator overloading
06. operator overloading06. operator overloading
06. operator overloading
Haresh Jaiswal
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
Ankur Pandey
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
Dhrumil Panchal
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Operator overloading and type conversion in cpp
Operator overloading and type conversion in cppOperator overloading and type conversion in cpp
Operator overloading and type conversion in cpp
rajshreemuthiah
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
 
Inline function
Inline functionInline function
Inline function
Tech_MX
 
Abstraction java
Abstraction javaAbstraction java
Abstraction java
MahinImran
 
Storage classes in c++
Storage classes in c++Storage classes in c++
Storage classes in c++
Jaspal Singh
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vishal Patil
 
operator overloading & type conversion in cpp
operator overloading & type conversion in cppoperator overloading & type conversion in cpp
operator overloading & type conversion in cpp
gourav kottawar
 
OOP in C++
OOP in C++OOP in C++
OOP in C++
ppd1961
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
06. operator overloading
06. operator overloading06. operator overloading
06. operator overloading
Haresh Jaiswal
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
Ankur Pandey
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
Dhrumil Panchal
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 

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

Namespace1
Namespace1Namespace1
Namespace1
zindadili
 
Namespace--defining same identifiers again
Namespace--defining same identifiers againNamespace--defining same identifiers again
Namespace--defining same identifiers again
Ajay Chimmani
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
SAFFI Ud Din Ahmad
 
srgoc
srgocsrgoc
srgoc
Gaurav Singh
 
Object Oriented Programming Using C++: C++ Namespaces.pptx
Object Oriented Programming Using C++: C++ Namespaces.pptxObject Oriented Programming Using C++: C++ Namespaces.pptx
Object Oriented Programming Using C++: C++ Namespaces.pptx
RashidFaridChishti
 
Programming in c plus plus4
Programming in c plus plus4Programming in c plus plus4
Programming in c plus plus4
AA Coaching Academy
 
Namespace
NamespaceNamespace
Namespace
abhay singh
 
OOP_EXPLAINED_example_of_cod_and_explainations.pdf
OOP_EXPLAINED_example_of_cod_and_explainations.pdfOOP_EXPLAINED_example_of_cod_and_explainations.pdf
OOP_EXPLAINED_example_of_cod_and_explainations.pdf
DerekDixmanChakowela
 
ECMAScript 2015
ECMAScript 2015ECMAScript 2015
ECMAScript 2015
Sebastian Pederiva
 
Briefly Rust
Briefly RustBriefly Rust
Briefly Rust
Daniele Esposti
 
iOS Session-2
iOS Session-2iOS Session-2
iOS Session-2
Hussain Behestee
 
C++ basics
C++ basicsC++ basics
C++ basics
AllsoftSolutions
 
C++primer
C++primerC++primer
C++primer
leonlongli
 
Object Oriented Programming (OOP) using C++ - Lecture 5
Object Oriented Programming (OOP) using C++ - Lecture 5Object Oriented Programming (OOP) using C++ - Lecture 5
Object Oriented Programming (OOP) using C++ - Lecture 5
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Perl tutorial final
Perl tutorial finalPerl tutorial final
Perl tutorial final
Ashoka Vanjare
 
Write a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdfWrite a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdf
jillisacebi75827
 
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
UNIT - 1-  Ood ddnwkjfnewcsdkjnjkfnskfn.pptxUNIT - 1-  Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
crazysamarth927
 
Rust vs C++
Rust vs C++Rust vs C++
Rust vs C++
corehard_by
 
Addressing Scenario
Addressing ScenarioAddressing Scenario
Addressing Scenario
Tara Hardin
 
Twitter Author Prediction from Tweets using Bayesian Network
Twitter Author Prediction from Tweets using Bayesian NetworkTwitter Author Prediction from Tweets using Bayesian Network
Twitter Author Prediction from Tweets using Bayesian Network
Hendy Irawan
 
Namespace--defining same identifiers again
Namespace--defining same identifiers againNamespace--defining same identifiers again
Namespace--defining same identifiers again
Ajay Chimmani
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
SAFFI Ud Din Ahmad
 
Object Oriented Programming Using C++: C++ Namespaces.pptx
Object Oriented Programming Using C++: C++ Namespaces.pptxObject Oriented Programming Using C++: C++ Namespaces.pptx
Object Oriented Programming Using C++: C++ Namespaces.pptx
RashidFaridChishti
 
OOP_EXPLAINED_example_of_cod_and_explainations.pdf
OOP_EXPLAINED_example_of_cod_and_explainations.pdfOOP_EXPLAINED_example_of_cod_and_explainations.pdf
OOP_EXPLAINED_example_of_cod_and_explainations.pdf
DerekDixmanChakowela
 
Write a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdfWrite a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdf
jillisacebi75827
 
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
UNIT - 1-  Ood ddnwkjfnewcsdkjnjkfnskfn.pptxUNIT - 1-  Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
crazysamarth927
 
Addressing Scenario
Addressing ScenarioAddressing Scenario
Addressing Scenario
Tara Hardin
 
Twitter Author Prediction from Tweets using Bayesian Network
Twitter Author Prediction from Tweets using Bayesian NetworkTwitter Author Prediction from Tweets using Bayesian Network
Twitter Author Prediction from Tweets using Bayesian Network
Hendy Irawan
 
Ad

Recently uploaded (20)

acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
ijflsjournal087
 
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
Reflections on Morality, Philosophy, and History
 
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation RateModeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Journal of Soft Computing in Civil Engineering
 
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink DisplayHow to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
CircuitDigest
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
DED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedungDED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedung
nabilarizqifadhilah1
 
Working with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to ImplementationWorking with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to Implementation
Alabama Transportation Assistance Program
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...
Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...
Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...
Journal of Soft Computing in Civil Engineering
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning ModelsMode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Journal of Soft Computing in Civil Engineering
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
ijflsjournal087
 
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink DisplayHow to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
CircuitDigest
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
DED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedungDED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedung
nabilarizqifadhilah1
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Ad

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
  翻译: