0% found this document useful (0 votes)
10 views7 pages

Lecture 1.3

The document explains the concept of namespaces in C++, which help prevent name conflicts in large projects by placing symbols in a named scope. It details how to define a namespace, call functions or variables within it, and utilize the 'using' directive to simplify code. Examples illustrate the practical application of namespaces and the output of functions from different namespaces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Lecture 1.3

The document explains the concept of namespaces in C++, which help prevent name conflicts in large projects by placing symbols in a named scope. It details how to define a namespace, call functions or variables within it, and utilize the 'using' directive to simplify code. Examples illustrate the practical application of namespaces and the output of functions from different namespaces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

CONTENTS

• Introduction to namespace

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

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

3
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; 4
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;

5
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;
}

6
Summary

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

You might also like