Professional Documents
Culture Documents
Course Objectives
2
Course Outcomes
CO Title Level
Number
Sr. Type of Assessment Weightage of actual Frequency of Task Final Weightage in Internal Remarks
No. Task conduct Assessment (Prorated
Marks)
5
Namespace
• Namespaces provide a method for preventing name
conflicts in large projects.
• Symbols declared inside a namespace block are placed
in a named scope that prevents them from being
mistaken for identically-named symbols in other scopes.
• Multiple namespace blocks with the same name are
allowed. All declarations within those blocks are
declared in the named scope.
6
Defining a Namespace
7
Example:
#include <iostream>
using namespace std;
// first name space
namespace first_space { Output:
void func() { Inside first_space
cout << "Inside first_space" << endl; Inside second_space
}
}
// second name space
namespace second_space {
void func() {
cout << "Inside second_space" << endl;
}
}
int main () {
// Calls function from first name space.
first_space::func();
8
The using directive
• You can also avoid prepending of namespaces with the using
namespace directive. This directive tells the compiler that the
subsequent code is making use of names in the specified namespace.
• The ‘using’ directive can also be used to refer to a particular item
within a namespace. For example, if the only part of the std
namespace that you intend to use is cout, you can refer to it as
follows −
using std::cout;
9
Example:
#include <iostream>
using namespace std;
// first name space
namespace first_space { Output:
void func() { Inside first_space
cout << "Inside first_space" << endl;
}
}
// second name space
namespace second_space {
void func() {
cout << "Inside second_space" << endl;
}
}
using namespace first_space;
int main () {
// This calls function from first name space.
func();
return 0;
}
10
Summary
11
Frequently Asked question
Q1 What is a namespace?
Answer: A namespace is designed to overcome this difficulty and is used as
additional information to differentiate similar functions, classes, variables etc.
with the 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.
12
Q2 Why is namespace important?
Answer: Consider a situation, when we have two persons with the same name, Zara, 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 the area, if they live
in different area or their mother’s or father’s name, etc.
Same situation can arise in your C++ applications. For example, you might be writing
some code that has a function called xyz() and there is another library available which
is also having same function xyz(). Now the compiler has no way of knowing which
version of xyz() function you are referring to within your code.
14
4. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
namespace
{
int var = 10;
}
int main()
{
cout<<var;
}
a) 10
b) Error
c) Some garbage value
d) Nothing but program runs perfectly
namespace A{
int var = 10;
namespace B{
int i = 15;
}
}
a) cout<<A::i;
b) cout<<B::i;
c) cout<<A::B::i;
d) cout<<i; 15
Discussion forum.
A deeper dive into namespaces
https://www.youtube.com/watch?v=ts1Eek5w7ZA
16
REFERENCES
TEXT BOOKS
T1 E Balagurusamy., “Object Oriented Programming in C++”, Tata McGraw-Hill.
T2 Robert Lafore, “Object Oriented Programming in C++”, Waite Group.
REFERENCE BOOKS
R1 Herbert Schildt , “C++- The Complete Reference”, Tata McGraw-Hill 2003, New
Delhi.
R2 Bjarne Stroustrup: “The C++ Programming Language” (4th Edition). Addison-Wesley.
R3 Ravichandran , “Programming with C++”,Tata McGraw-Hill Education.
R4 Joyce M. Farrell,” Object Oriented Programming Using C++”, Learning.
R5 Programming Languages: Design and Implementation (4th Edition), by Terrence W.
Pratt, Marvin V. Zelkowitz, Pearson.
R6 Programming Language Pragmatics, Third Edition, by Michael L. Scott, Morgan
Kaufmann.
Websites:
1. https://en.cppreference.com/w/cpp/language/namespace
2. https://www.tutorialspoint.com/cplusplus/cpp_namespaces.htm
3. https://www.sanfoundry.com/cplusplus-programming-questions-answers-namespaces-2/
17
THANK YOU