Lecture 1.3
Lecture 1.3
Lecture 1.3
Course Objectives
2
Course Outcomes
CO Course Outcome
Number
CO1
Understand the concepts of object-oriented programming
including programming process and compilation process.
CO2
Apply different techniques to decompose a problem and
programmed a solution with its sub modules.
CO3
Analyze and explain the behavior of simple programs involving the
programming addressed in the course.
CO4
Implement and evaluate the programs using the syntax and
semantics of object-oriented programming.
CO5
Design the solution of real-world problems in order to determine
that the program performs as expected.
3
Scheme of Evaluation
4
CONTENTS
• Introduction to namespace
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
Inside second_space
cout << "Inside first_space" << endl;
}
}
// second name space
namespace second_space {
void func() {
cout << "Inside second_space" << endl;
}
}
int main () {
// Calls function from first name space.
first_space::func();
9
Example:
#include <iostream>
using namespace std;
// first name space
namespace first_space { Output:
Inside first_space
void func() {
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
17
3. https://www.sanfoundry.com/cplusplus-programming-questions-answers-namespaces-2/
THANK YOU