SlideShare a Scribd company logo
1
 Consider a situation, when we have two persons with the same name, M. Ali,
in the same class. Whenever we need to differentiate them definitely we
would have to use some additional information along with their name, like
either Reg. Number or their Father’s Name, etc.
 Same situation can arise in your C++ program. For example, you might be
writing some code that has a function called print() and there is another
library available which is also having same function print(). Now the compiler
has no way of knowing which version of print() function you are referring to
within your code.
 A namespace is designed to overcome this difficulty and is used as additional
information to differentiate similar functions, classes, variables etc. with the
Introduction
 same name available in different libraries. Using namespace, you can define
the context in which names are defined. In essence, a namespace defines a
scope.
 A namespace definition begins with the keyword namespace followed by the
namespace name.
 To call the namespace-enabled version of either function or variable, write
scope resolution operator (::) between the namespace and the code:
Introduction
namespace namespace_name {
// code declarations
}
namespace_name::code
#include <iostream>
// Define a namespace called "MyNamespace"
namespace MyNamespace {
// Define a simple function within the namespace
void displayMessage() {
std::cout << "Hello from MyNamespace!" << std::endl;
}
}
int main() {
// Call the function from the namespace without using the namespace prefix
MyNamespace::displayMessage();
return 0;
}
Example 1: Using namespace
#include <iostream>
#include <stdlib.h>
namespace Cartesian_Coordinates {
double x = 11.2;
double y = 8.7;
void Show(){
std::cout<< "x = " << x
<< ", y = "<< y
<< std::endl;
}
}
Example 2: Using two namespaces
namespace Spherical_Coordinates {
double r = 14.18;
double theta = 37.84;
void Show(){
std::cout<< "r = " << r
<< ", Theta = " << theta
<< std::endl;
}
}
int main() {
Cartesian_Coordinates::Show();
Spherical_Coordinates::Show();
system("pause");
return 0;
}
Page 1 Page 2
#include <iostream>
#include <stdlib.h>
namespace Cartesian_Coordinates {
double x = 11.2;
double y = 8.7;
void Show(){
std::cout<< "x = " << x
<< ", y = "<< y
<< std::endl;
}
}
Example 3: The using directive
namespace Spherical_Coordinates {
double r = 14.18;
double theta = 37.84;
void Show(){
std::cout<< "r = " << r
<< ", Theta = " << theta
<< std::endl;
}
}
using namespace Spherical_Coordinates;
int main() {
Show(); // it will call Show from
// Spherical_Coordinates namespace
system("pause");
return 0;
}
Page 1 Page 2
#include <iostream>
// Define a namespace called "Shapes"
namespace Shapes {
class Circle {
private:
double radius;
public:
Circle(double radius):radius(radius) {}
double calculateArea() const {
return 3.14 * radius * radius;
}
};
}
Example 4: Using class in a namespace
int main() {
// Create an instance of the Circle class
// from the Shapes namespace
Shapes::Circle myCircle(5.0);
std::cout << "Area of the circle: "
<< myCircle.calculateArea()
<< std::endl;
return 0;
}
Page 1 Page 2
#include <iostream>
using namespace std;
namespace Geometry { // Define a namespace called "Geometry"
class Point { // Define a class called "Point" to represent a point in 2D space
public:
int x,y;
Point(int x, int y) : x(x), y(y) {}
void Show() const {
cout << "(" << x << ", " << y << ")";
}
};
class Rectangle {
private:
Point topLeft, bottomRight;
public:
Rectangle(Point TL, Point BR):topLeft(TL), bottomRight(BR) {}
void Draw() const {
int height = bottomRight.x - topLeft.x;
int width = bottomRight.y - topLeft.y;
Example 5: Using two classes in a namespace
Page 1
for(int row = 0 ; row < height ; row++){
for(int col = 0 ; col< width ; col++){
cout<<"*";
}
cout<<"n";
}
}
};
}
int main() {
// Create instances of classes from the Geometry namespace
Geometry::Point topLeft(5, 7); Geometry::Point bottomRight(8, 11);
cout << "Top Left = "; topLeft.Show(); cout <<"n";
cout << "Bottom Right = "; bottomRight.Show(); cout <<"n";
Geometry::Rectangle rectangle(topLeft, bottomRight);
rectangle.Draw();
return 0;
}
Example 5: Using two classes in a namespace
Page 2
 Namespaces can be nested where you can define one namespace inside
another name space.
 You can access members of nested namespace by using resolution operators.
Nested Namespaces
namespace namespace_name1 {
// code declarations
namespace namespace_name2 {
// code declarations
}
}
// to access members of namespace_name2
using namespace namespace_name1::namespace_name2;
// to access members of namespace:name1
using namespace namespace_name1;
#include <iostream>
#include <stdlib.h>
using namespace std;
// first name space
namespace first_space {
void func() {
cout << "Inside first_space"
<< endl;
}
// second name space
namespace second_space {
void func() {
cout << "Inside second_space"
<< endl;
}
}
}
Example 6: Nested Namespaces
using namespace first_space::second_space;
int main () {
// This will call function from
// second name space.
func();
system("PAUSE");
return 0;
}
// now try with
// using namespace first_space
Page 1 Page 2

More Related Content

Similar to Object Oriented Programming Using C++: C++ Namespaces.pptx

What's New In C# 7
What's New In C# 7What's New In C# 7
What's New In C# 7
Paulo Morgado
 
The Ring programming language version 1.8 book - Part 94 of 202
The Ring programming language version 1.8 book - Part 94 of 202The Ring programming language version 1.8 book - Part 94 of 202
The Ring programming language version 1.8 book - Part 94 of 202
Mahmoud Samir Fayed
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Chris Adamson
 
Bc0037
Bc0037Bc0037
Bc0037
hayerpa
 
The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88
Mahmoud Samir Fayed
 
Namespaces
NamespacesNamespaces
Namespaces
zindadili
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshop
BAINIDA
 
Back to the Future with TypeScript
Back to the Future with TypeScriptBack to the Future with TypeScript
Back to the Future with TypeScript
Aleš Najmann
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
Dmitry Sheiko
 
Python programing
Python programingPython programing
Python programing
hamzagame
 
ES6 General Introduction
ES6 General IntroductionES6 General Introduction
ES6 General Introduction
Thomas Johnston
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003
R696
 
srgoc
srgocsrgoc
ES6 is Nigh
ES6 is NighES6 is Nigh
ES6 is Nigh
Domenic Denicola
 
OOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxOOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptx
SirRafiLectures
 
C++ Function
C++ FunctionC++ Function
C++ Function
Hajar
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
Tomasz Kowalczewski
 
Op ps
Op psOp ps
C# programming
C# programming C# programming
C# programming
umesh patil
 
C++ theory
C++ theoryC++ theory
C++ theory
Shyam Khant
 

Similar to Object Oriented Programming Using C++: C++ Namespaces.pptx (20)

What's New In C# 7
What's New In C# 7What's New In C# 7
What's New In C# 7
 
The Ring programming language version 1.8 book - Part 94 of 202
The Ring programming language version 1.8 book - Part 94 of 202The Ring programming language version 1.8 book - Part 94 of 202
The Ring programming language version 1.8 book - Part 94 of 202
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
 
Bc0037
Bc0037Bc0037
Bc0037
 
The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88
 
Namespaces
NamespacesNamespaces
Namespaces
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshop
 
Back to the Future with TypeScript
Back to the Future with TypeScriptBack to the Future with TypeScript
Back to the Future with TypeScript
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
Python programing
Python programingPython programing
Python programing
 
ES6 General Introduction
ES6 General IntroductionES6 General Introduction
ES6 General Introduction
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003
 
srgoc
srgocsrgoc
srgoc
 
ES6 is Nigh
ES6 is NighES6 is Nigh
ES6 is Nigh
 
OOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxOOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptx
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
 
Op ps
Op psOp ps
Op ps
 
C# programming
C# programming C# programming
C# programming
 
C++ theory
C++ theoryC++ theory
C++ theory
 

More from RashidFaridChishti

Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
RashidFaridChishti
 
Object Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docxObject Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docx
RashidFaridChishti
 
Lab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docxLab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docx
RashidFaridChishti
 
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptxData Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxData Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptxData Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptxData Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptxData Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptxData Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxData Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptxData Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptx
RashidFaridChishti
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxObject Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
RashidFaridChishti
 

More from RashidFaridChishti (20)

Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
 
Object Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docxObject Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docx
 
Lab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docxLab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docx
 
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptxData Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxData Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptx
 
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptxData Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptx
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
 
Data Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptxData Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptx
 
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptxData Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
 
Data Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptxData Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptx
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
 
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxData Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptx
 
Data Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptxData Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptx
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxObject Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
 

Recently uploaded

Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
Mahmoud Morsy
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
Madan Karki
 
artificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptxartificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptx
GauravCar
 
Welding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdfWelding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdf
AjmalKhan50578
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
KrishnaveniKrishnara1
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
Madan Karki
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
gowrishankartb2005
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
abbyasa1014
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
Prakhyath Rai
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
Roger Rozario
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
AI assisted telemedicine KIOSK for Rural India.pptx
AI assisted telemedicine KIOSK for Rural India.pptxAI assisted telemedicine KIOSK for Rural India.pptx
AI assisted telemedicine KIOSK for Rural India.pptx
architagupta876
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
UReason
 

Recently uploaded (20)

Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
 
artificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptxartificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptx
 
Welding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdfWelding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdf
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
AI assisted telemedicine KIOSK for Rural India.pptx
AI assisted telemedicine KIOSK for Rural India.pptxAI assisted telemedicine KIOSK for Rural India.pptx
AI assisted telemedicine KIOSK for Rural India.pptx
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
 

Object Oriented Programming Using C++: C++ Namespaces.pptx

  • 1. 1
  • 2.  Consider a situation, when we have two persons with the same name, M. Ali, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either Reg. Number or their Father’s Name, etc.  Same situation can arise in your C++ program. For example, you might be writing some code that has a function called print() and there is another library available which is also having same function print(). Now the compiler has no way of knowing which version of print() function you are referring to within your code.  A namespace is designed to overcome this difficulty and is used as additional information to differentiate similar functions, classes, variables etc. with the Introduction
  • 3.  same name available in different libraries. Using namespace, you can define the context in which names are defined. In essence, a namespace defines a scope.  A namespace definition begins with the keyword namespace followed by the namespace name.  To call the namespace-enabled version of either function or variable, write scope resolution operator (::) between the namespace and the code: Introduction namespace namespace_name { // code declarations } namespace_name::code
  • 4. #include <iostream> // Define a namespace called "MyNamespace" namespace MyNamespace { // Define a simple function within the namespace void displayMessage() { std::cout << "Hello from MyNamespace!" << std::endl; } } int main() { // Call the function from the namespace without using the namespace prefix MyNamespace::displayMessage(); return 0; } Example 1: Using namespace
  • 5. #include <iostream> #include <stdlib.h> namespace Cartesian_Coordinates { double x = 11.2; double y = 8.7; void Show(){ std::cout<< "x = " << x << ", y = "<< y << std::endl; } } Example 2: Using two namespaces namespace Spherical_Coordinates { double r = 14.18; double theta = 37.84; void Show(){ std::cout<< "r = " << r << ", Theta = " << theta << std::endl; } } int main() { Cartesian_Coordinates::Show(); Spherical_Coordinates::Show(); system("pause"); return 0; } Page 1 Page 2
  • 6. #include <iostream> #include <stdlib.h> namespace Cartesian_Coordinates { double x = 11.2; double y = 8.7; void Show(){ std::cout<< "x = " << x << ", y = "<< y << std::endl; } } Example 3: The using directive namespace Spherical_Coordinates { double r = 14.18; double theta = 37.84; void Show(){ std::cout<< "r = " << r << ", Theta = " << theta << std::endl; } } using namespace Spherical_Coordinates; int main() { Show(); // it will call Show from // Spherical_Coordinates namespace system("pause"); return 0; } Page 1 Page 2
  • 7. #include <iostream> // Define a namespace called "Shapes" namespace Shapes { class Circle { private: double radius; public: Circle(double radius):radius(radius) {} double calculateArea() const { return 3.14 * radius * radius; } }; } Example 4: Using class in a namespace int main() { // Create an instance of the Circle class // from the Shapes namespace Shapes::Circle myCircle(5.0); std::cout << "Area of the circle: " << myCircle.calculateArea() << std::endl; return 0; } Page 1 Page 2
  • 8. #include <iostream> using namespace std; namespace Geometry { // Define a namespace called "Geometry" class Point { // Define a class called "Point" to represent a point in 2D space public: int x,y; Point(int x, int y) : x(x), y(y) {} void Show() const { cout << "(" << x << ", " << y << ")"; } }; class Rectangle { private: Point topLeft, bottomRight; public: Rectangle(Point TL, Point BR):topLeft(TL), bottomRight(BR) {} void Draw() const { int height = bottomRight.x - topLeft.x; int width = bottomRight.y - topLeft.y; Example 5: Using two classes in a namespace Page 1
  • 9. for(int row = 0 ; row < height ; row++){ for(int col = 0 ; col< width ; col++){ cout<<"*"; } cout<<"n"; } } }; } int main() { // Create instances of classes from the Geometry namespace Geometry::Point topLeft(5, 7); Geometry::Point bottomRight(8, 11); cout << "Top Left = "; topLeft.Show(); cout <<"n"; cout << "Bottom Right = "; bottomRight.Show(); cout <<"n"; Geometry::Rectangle rectangle(topLeft, bottomRight); rectangle.Draw(); return 0; } Example 5: Using two classes in a namespace Page 2
  • 10.  Namespaces can be nested where you can define one namespace inside another name space.  You can access members of nested namespace by using resolution operators. Nested Namespaces namespace namespace_name1 { // code declarations namespace namespace_name2 { // code declarations } } // to access members of namespace_name2 using namespace namespace_name1::namespace_name2; // to access members of namespace:name1 using namespace namespace_name1;
  • 11. #include <iostream> #include <stdlib.h> using namespace std; // first name space namespace first_space { void func() { cout << "Inside first_space" << endl; } // second name space namespace second_space { void func() { cout << "Inside second_space" << endl; } } } Example 6: Nested Namespaces using namespace first_space::second_space; int main () { // This will call function from // second name space. func(); system("PAUSE"); return 0; } // now try with // using namespace first_space Page 1 Page 2