Skip to main content

namespace in c++

Hello friends, in this post today, we will learn about namespace in c++ and how to use in our program. so let's start....

Namespace in c++, namespace
namespace in c++





 Namespace:-

It defines a scope for the identifiers that are utilized in a program.


It puts the names of its members during a distinct space in order that they don’t conflict with the names in others namespace or global namespace.


A namespace definition are often continued and extended over multiple files, they're not redefined or overridden.


Namespace definition doesn’t terminates with a semicolon like in class data type definition.


Remember namespace is not a class, you cannot create instance of namespace.


The namespace definition must be done at worldwide scope, or nested insides another namespace.


Using keyword allows you to import a whole namespace into your program with a worldwide scope.


It are often wont to import a namespace into another namespace or any program.


• For using identifiers defined in namespace using directive is employed as follows: using namespace std;


std is that the namesace where ANSI C++ standard class libraries are defined.


All ANSI C++ programs must include this directive that is std.


This may bring all the identifiers defined in std to the present global scope.


The syntax of namespaces is:

   namespace namespace_identifier

     {

       entities

     }

Where namespace_identifier is any valid identifier and entities is that the collection of classes, objects and functions that are included within the namespace.


 For example:

namespace First_Namespace

{

int a, b, c;

}

In this case, the variables a, b and c are normal variables declared within a namespace called First_Namespace.

 In order to access these variables from outside the First_Namespace namespace we have to use the scope resolution operator (::). For example, to access the previous variables from outside First_Namespace we can write:

First_Namespace::a

First_Namespace::b

First_Namespace::c

The functionality of namespaces is particularly useful within the case that there's an opportunity that a worldwide object or function uses an equivalent identifier as another one, causing redefinition errors.

Example:

// namespaces
#include <iostream>
using namespace std;

 namespace Myspace //Myspace is name of namespace
 {
     int a=67;
     void f1(); //Function declaration
     class A
    {
       public:
      void fun1();
    };
 }

 void Myspace::f1()
 {
   cout<<"Namespace Demo! f1() function";
 }

 void Myspace::A::fun1()
 {
   cout<<"Thankyou: fun1() function ";
 }

using namespace Myspace;
int main()
{
   
   f1();
   cout<<endl;
   A obj;
   obj.fun1();
   cout<<endl;
   cout<<a;
  return 0;
}

Output:-

Namespace Demo! f1() function
Thankyou: fun1() function 
67


I hope that whatever information I have given in this post today, you have liked it and you have understood it.so keep learning and wait for the next post that will help you to increase your knowledge with the something new information.

Thank you so much for reading. And take care about yourself and your family.



Comments

Popular posts from this blog

Constructor in c++

Hello friends, in this post today, we learn about  what is constructor in c++ and types of constructor . so lets start... Constructor in c++ Constructor:-   A constructor is a “special” member function which initializes the objects of class.  Constructor is used to solve problem of initialization. Properties of constructor:- Constructors are used to construct the object of the class. Constructor name must be same as class name. Constructor is invoked automatically when objects of the class are created. The constructors are always declared in the public section. If declared in the private section then objects are can only be created inside the member functions but serve no purpose. It must be an instance member function , that is, it can never be static. Constructors  do not have any return type not even void so they cannot return any value. Constructor can also be overloaded. Constructors cannot be inherited, but they can be called from the constructors of...

Function with Default arguments in c++

Hello friends, i n this post today, we learn about   Function with Default arguments in c++ .  so lets start... Function with default arguments  Function with default arguments:- In C ++ it is possible for a function not to specify all its arguments. Some of the arguments  may be specified their default values at the time of declaring the function.   Default values are specified when the function is declared. We must add default arguments from right to left. We cannot provide a default value to a particular argument in the middle of an argument list. Default arguments are useful in situations where some arguments always have the same value. For Example, passing marks. In a function with default argument, if one argument is default, all successive arguments  must be default. We cannot provide default values in the middle of the arguments or towards  l eft side. We provide few examples: 1. void fun(int x, int y = 20, int z=35); (valid) 2. void fun(int x,...

Difference between procedure and object oriented programming in c++

Hello friends, in this post today, we will learn about what is the Difference between procedure and object oriented programming in c++ . so let's start.. .. Object Oriented Programming is programming paradigm that represents concepts as objects that has data fields and associated procedures known as methods. Procedure Oriented Programming (POP)  1. Emphasis is on doing things not on data, means it is function driven. 2. Main focus is on the function and procedures that operate on data. 3. Top Down approach in program design. 4. Large programs are divided programs known as functions into smaller. 5. Most of the functions share global data. 6. Data moves openly in the system from one function to another function. 7. Adding of data and function is difficult. 8. We cannot declare namespace directly.  9. Concepts like inheritance, polymorphism, data encapsulation, abstraction, access specifiers are not      available.  10. Examples: C, Fortran, Pascal, etc… proc...