C++ - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Namespace in C++

Namespace in C++

shape Introduction

In a class, suppose there are few students in which two of them have same name. If comes a situation to call any one of those students, one should make a difference in calling the name by attaching surname, roll no. to the original name. The same problem arises in C++, when two or more classes, functions, methods, objects appear with the same names. Here, namespace becomes the solution by making the difference between similar names.

shape Description

Namespace in CPP is the collection of identifiers and is used for two reasons.
  1. To bring the classes or objects or functions performing similar task together.
  2. To show the difference while referring to same identifiers by providing extra information.
Namespaces are declared using the keyword "namespace".Everything that's part of the standard library is inside the standard namespace and things with exactly the same name can be used in the namespace, if needed. Now,there will be no problem with having to type the name of the namespace every single time classes are used when own string or own console out are not written.

shape Syntax

namespace namespace_name { //namespace content }
To call the declared functions in the namespaces, the symbol:: with namespace_name before it and function name after ::.

shape Example

[c] #include <iostream> using namespace std; // first name space namespace first_name { void splessons() { cout << "The code inside first name space is executed" << endl; } } // second name space namespace second_name { void splessons() { cout << "The code inside second name space is executed" << endl; } } int main () { // Calls function in first name space first_name::splessons(); // Calls function in second name space. second_name::splessons(); return 0; }[/c] Output [c] The code inside first name space is executed The code inside second name space is executed[/c]

Directive "using"

shape Description

Instead of specifying name of namespace before every function of that namespace, one can use "using namespace" directive at the beginning of the program.The usingkeyword tells the compiler that the code written calls the functions in the namespaces declared already. Using never has anything to do with scope. Using is only about these namespace directives, it doesn't link new code in or add new references, anything it's simply telling the compiler, whenever cout is used and quickly go and see if it is std::cout. The "using namespace" directive can also be used to refer only a single function in a group of functions.For example, "cin" function can be called as below using std::in

shape Examples

[c] #include <iostream> using std::cin; using std::cout; int main () { int i; std::cout << " Enter a value" << std::endl; std::cin >> i; std::cout << "You entered" << i ; return 0; }[/c] Output [c] Enter a value 10 you entered 10[/c]

Same namespace-Multiple locations

shape Description

Consider there is a namespace with a function declared in it. Another function is declared with same namespace name but with different function name. Now, all the functions declared under that namespace can be considered as functions with single namespace. A separate file can be created for each function.

shape Examples

[c] #include<iostream> using namespace std; namespace First_name { void func() { cout << "Function in First_name is called" << endl; } } namespace Second_name { void func() { cout << "Function in Second_name is called" << endl; } } namespace First_name { int i = 10; } namespace First_name { double j = 1.115; } int main() { First_name::func(); cout << "The value of i is " << First_name::i << endl; cout << "The value of i is " << First_name::j << endl; Second_name::func(); return 0; } [/c] Output [c] Function in First_name is called The value of i is 10 The value of i is 1.115 Function in Second_name is called[/c]

Nested namespace

shape Description

Like nested if loops, nested namespaces are also present. A namespace in a namespace can be called as "Nested namespace".

shape Example

[c] #include<iostream> using namespace std; namespace Outer { void func() { cout << "Function from Outer namespace is called" << endl; } namespace Inner { void func() { cout << "Function from Outer namespace is called" << endl; } } } int main() { Outer::Inner::func(); return 0; } [/c] Output [c] Function from Outer namespace is called[/c]

Alias Namespaces

shape Description

If the names of Namespace in CPP is more in length and difficult to declare in the program, alias names can be used for namespaces.

shape Syntax

Namespace New_name = Old_name;
Eg : namespace AB = A::B;

Summary

shape Key Points

  • Namespace in CPP is the collection of identifiers.
  • Scope resolution operator is used to refer a namespace.
  • Same namespace can be called in multiple locations.

shape Programming Tips

Be careful while using namespaces in nesting format because confusion occurs.