You are on page 1of 18

INSTITUTE - UIS

DEPARTMENT-  Mathematics Division


Bachelor of Science
Subject Name: Object Oriented Programming using C++
Code:20SMT103
Unit-1

Topic: Introduction to namespace DISCOVER . LEARN . EMPOWER


Object Oriented
Programming
using C++

Course Objectives

• To enable the students to understand various stages and constructs


of C++ programming language and relate them to engineering
programming problems.
• To improve their ability to analyze and address variety of problems
in programming domains.

2
Course Outcomes
CO Title Level
Number

CO1 Provide the environment that allows students to Understand


understand object-oriented programming Concepts.  

CO2 Demonstrate basic experimental skills for differentiating Remember


between object-oriented and procedural programming  
paradigms and the advantages of object-oriented
programs.
CO3 Demonstrate their coding skill on complex programming Understand
concepts and use it for generating solutions for
engineering and mathematical problems.

CO4 Develop skills to understand the application of classes, Understand


objects, constructors, destructors, inheritance, operator  
overloading and polymorphism, pointers, virtual
functions, exception handling, file operations and
handling.
3
Scheme of Evaluation

Sr. Type of Assessment Weightage of actual Frequency of Task Final Weightage in Internal Remarks
No. Task conduct Assessment (Prorated
Marks)

1. Assignment* 10 marks of One Per Unit 10 marks As applicable to


each assignment course types depicted
above.
2. Time Bound 12 marks for each One per Unit 4 marks As applicable to
Surprise test course types
Test depicted above.
3. Quiz 4 marks of each quiz 2 per Unit 4marks As applicable to
course types
depicted above.
4. Mid-Semester Test** 20 marks for one 2 per semester 20 marks As applicable to
MST. course types
depicted above.
5. Presentation***     Non Graded: Engagement Only for Self Study
Task MNGCourses.

6. Homework NA One per lecture topic Non-Graded: Engagement As applicable to


(of 2 Task course types
questions) depicted above.
7. Discussion Forum NA One per Non Graded: Engagement As applicable to
Chapter Task course types depicted
above.
8. Attendance and NA NA 2 marks  
Engagement Score
on BB
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

• A namespace definition begins with the


keyword namespace followed by the namespace name as
follows −

namespace namespace_name { // code declarations }

• To call the namespace-enabled version of either function or


variable, prepend (::) the namespace name as follows −

name::code; // code could be variable or function.

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();

// Calls function from second name space.


second_space::func();
return 0;
}

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

In this lecture we have discussed about Namespaces in C++

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.

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.
13
Assessment Questions:
1. Which operator is used for accessing a member of namespace?
a) :
b) ::
c) ->
d) .

2. Pick the incorrect statement for namespaces in C++.


a) Namespace declarations are always global scope
b) Keyword namespace is used at the starting of a namespace definition
c) Namespace has access specifiers like private or public
d) Namespace definitions can be nested

3. What is the correct syntax of defining a namespace?


a) namespace name{}
b) Namespace name{};
c) namespace name{};
d) typedef namespace name{} NAME

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

5. How to print the value of the i variable inside namespace B?

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

You might also like