Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lecture 1.3

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

INSTITUTE - UIE

DEPARTMENT- ACADEMIC UNIT-2


Bachelor of Engineering (Computer Science & Engineering)
Subject Name: Object Oriented Programming using C++
Code:22CSH-103

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 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

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

// 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:
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

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
17
3. https://www.sanfoundry.com/cplusplus-programming-questions-answers-namespaces-2/
THANK YOU

You might also like