SlideShare a Scribd company logo
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
Most read
5
Syntax
namespace Name_Space_Name
{
Some_Code
int x=4;
}
To print value of x in
main()
cout<<Name_Space_
Name::x;
Most read
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
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

What's hot (20)

Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vishal Patil
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
Tech_MX
 
Functions in c
Functions in cFunctions in c
Functions in c
sunila tharagaturi
 
Strings
StringsStrings
Strings
Mitali Chugh
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
Haresh Jaiswal
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
Tasnima Hamid
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
lalithambiga kamaraj
 
Intro to c++
Intro to c++Intro to c++
Intro to c++
temkin abdlkader
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
Venkata.Manish Reddy
 
What are variables and keywords in c++
What are variables and keywords in c++What are variables and keywords in c++
What are variables and keywords in c++
Abdul Hafeez
 
C presentation book
C presentation bookC presentation book
C presentation book
krunal1210
 
Friend function
Friend functionFriend function
Friend function
zindadili
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressions
vishaljot_kaur
 
C# Constructors
C# ConstructorsC# Constructors
C# Constructors
Prem Kumar Badri
 
Manipulators in c++
Manipulators in c++Manipulators in c++
Manipulators in c++
Ashok Raj
 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C Language
Nitesh Kumar Pandey
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Kamal Acharya
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vishal Patil
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
Tech_MX
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
Tasnima Hamid
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
lalithambiga kamaraj
 
What are variables and keywords in c++
What are variables and keywords in c++What are variables and keywords in c++
What are variables and keywords in c++
Abdul Hafeez
 
C presentation book
C presentation bookC presentation book
C presentation book
krunal1210
 
Friend function
Friend functionFriend function
Friend function
zindadili
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressions
vishaljot_kaur
 
Manipulators in c++
Manipulators in c++Manipulators in c++
Manipulators in c++
Ashok Raj
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Kamal Acharya
 

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
 
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
 
Savitch Ch 12
Savitch Ch 12Savitch Ch 12
Savitch Ch 12
Terry Yoast
 
Savitch ch 12
Savitch ch 12Savitch ch 12
Savitch ch 12
Terry Yoast
 
C++ Constructs.pptx
C++ Constructs.pptxC++ Constructs.pptx
C++ Constructs.pptx
LakshyaChauhan21
 
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
 
Namespace.pdf
Namespace.pdfNamespace.pdf
Namespace.pdf
João Manuel Correia
 
Introduction-to-Java-Namespaces.pptx ejb
Introduction-to-Java-Namespaces.pptx ejbIntroduction-to-Java-Namespaces.pptx ejb
Introduction-to-Java-Namespaces.pptx ejb
momnatanveer321
 
data structure book in c++ and c in easy wording
data structure book in c++  and c in easy wordingdata structure book in c++  and c in easy wording
data structure book in c++ and c in easy wording
yhrcxd8wpm
 
Learning c++
Learning c++Learning c++
Learning c++
Bala Lavanya
 
C++ Presentation
C++ PresentationC++ Presentation
C++ Presentation
Carson Wilber
 
Namespace
NamespaceNamespace
Namespace
abhay singh
 
Learn C# programming - Interfaces & Namespaces
Learn C# programming - Interfaces & NamespacesLearn C# programming - Interfaces & Namespaces
Learn C# programming - Interfaces & Namespaces
Eng Teong Cheah
 
Chap 2 c++
Chap 2 c++Chap 2 c++
Chap 2 c++
Widad Jamaluddin
 
C++ LectuNSVAHDVQwyfkyuQWVHGWQUDKFEre-14.pptx
C++ LectuNSVAHDVQwyfkyuQWVHGWQUDKFEre-14.pptxC++ LectuNSVAHDVQwyfkyuQWVHGWQUDKFEre-14.pptx
C++ LectuNSVAHDVQwyfkyuQWVHGWQUDKFEre-14.pptx
ApoorvMalviya2
 
What's in a Name?
What's in a Name?What's in a Name?
What's in a Name?
Kevlin Henney
 
麻省理工C++公开教学课程(二)
麻省理工C++公开教学课程(二)麻省理工C++公开教学课程(二)
麻省理工C++公开教学课程(二)
ProCharm
 
C++ process new
C++ process newC++ process new
C++ process new
敬倫 林
 
Namespace inPHP
Namespace inPHP Namespace inPHP
Namespace inPHP
Tamaghna Banerjee
 
Namespace--defining same identifiers again
Namespace--defining same identifiers againNamespace--defining same identifiers again
Namespace--defining same identifiers again
Ajay Chimmani
 
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
 
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
 
Introduction-to-Java-Namespaces.pptx ejb
Introduction-to-Java-Namespaces.pptx ejbIntroduction-to-Java-Namespaces.pptx ejb
Introduction-to-Java-Namespaces.pptx ejb
momnatanveer321
 
data structure book in c++ and c in easy wording
data structure book in c++  and c in easy wordingdata structure book in c++  and c in easy wording
data structure book in c++ and c in easy wording
yhrcxd8wpm
 
Learn C# programming - Interfaces & Namespaces
Learn C# programming - Interfaces & NamespacesLearn C# programming - Interfaces & Namespaces
Learn C# programming - Interfaces & Namespaces
Eng Teong Cheah
 
C++ LectuNSVAHDVQwyfkyuQWVHGWQUDKFEre-14.pptx
C++ LectuNSVAHDVQwyfkyuQWVHGWQUDKFEre-14.pptxC++ LectuNSVAHDVQwyfkyuQWVHGWQUDKFEre-14.pptx
C++ LectuNSVAHDVQwyfkyuQWVHGWQUDKFEre-14.pptx
ApoorvMalviya2
 
麻省理工C++公开教学课程(二)
麻省理工C++公开教学课程(二)麻省理工C++公开教学课程(二)
麻省理工C++公开教学课程(二)
ProCharm
 
C++ process new
C++ process newC++ process new
C++ process new
敬倫 林
 
Ad

Recently uploaded (20)

Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)
elelijjournal653
 
Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.
Sowndarya6
 
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdfIrja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus
 
Transformimet e sinjaleve numerike duke perdorur transformimet
Transformimet  e sinjaleve numerike duke perdorur transformimetTransformimet  e sinjaleve numerike duke perdorur transformimet
Transformimet e sinjaleve numerike duke perdorur transformimet
IndritEnesi1
 
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
ijscai
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
gerogepatton
 
SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair KitSEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
BeHappy728244
 
Computer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdfComputer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdf
kumarprem6767merp
 
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra OpticaPruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
OmarAlfredoDelCastil
 
FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.
maldonadocesarmanuel
 
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbbTree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
RATNANITINPATIL
 
Artificial Power 2025 raport krajobrazowy
Artificial Power 2025 raport krajobrazowyArtificial Power 2025 raport krajobrazowy
Artificial Power 2025 raport krajobrazowy
dominikamizerska1
 
Call For Papers - International Journal on Natural Language Computing (IJNLC)
Call For Papers - International Journal on Natural Language Computing (IJNLC)Call For Papers - International Journal on Natural Language Computing (IJNLC)
Call For Papers - International Journal on Natural Language Computing (IJNLC)
kevig
 
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
sebastianku31
 
22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank
Guru Nanak Technical Institutions
 
Axial Capacity Estimation of FRP-strengthened Corroded Concrete Columns
Axial Capacity Estimation of FRP-strengthened Corroded Concrete ColumnsAxial Capacity Estimation of FRP-strengthened Corroded Concrete Columns
Axial Capacity Estimation of FRP-strengthened Corroded Concrete Columns
Journal of Soft Computing in Civil Engineering
 
ANFIS Models with Subtractive Clustering and Fuzzy C-Mean Clustering Techniqu...
ANFIS Models with Subtractive Clustering and Fuzzy C-Mean Clustering Techniqu...ANFIS Models with Subtractive Clustering and Fuzzy C-Mean Clustering Techniqu...
ANFIS Models with Subtractive Clustering and Fuzzy C-Mean Clustering Techniqu...
Journal of Soft Computing in Civil Engineering
 
Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)
elelijjournal653
 
Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.
Sowndarya6
 
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdfIrja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus
 
Transformimet e sinjaleve numerike duke perdorur transformimet
Transformimet  e sinjaleve numerike duke perdorur transformimetTransformimet  e sinjaleve numerike duke perdorur transformimet
Transformimet e sinjaleve numerike duke perdorur transformimet
IndritEnesi1
 
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
Top Cite Articles- International Journal on Soft Computing, Artificial Intell...
ijscai
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
gerogepatton
 
SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair KitSEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
Direct Current circuitsDirect Current circuitsDirect Current circuitsDirect C...
BeHappy728244
 
Computer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdfComputer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdf
kumarprem6767merp
 
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra OpticaPruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
OmarAlfredoDelCastil
 
FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.
maldonadocesarmanuel
 
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbbTree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
RATNANITINPATIL
 
Artificial Power 2025 raport krajobrazowy
Artificial Power 2025 raport krajobrazowyArtificial Power 2025 raport krajobrazowy
Artificial Power 2025 raport krajobrazowy
dominikamizerska1
 
Call For Papers - International Journal on Natural Language Computing (IJNLC)
Call For Papers - International Journal on Natural Language Computing (IJNLC)Call For Papers - International Journal on Natural Language Computing (IJNLC)
Call For Papers - International Journal on Natural Language Computing (IJNLC)
kevig
 
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
sebastianku31
 
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