19 Sept 2017

Namespace in c++

Consider following C++ program.
// A program to demonstrate need of namespace
int main()
{
    int value;
    value = 0;
    double value; // Error here
    value = 0.0;
}Output :
Compiler Error:
'value' has a previous declaration as 'int value'
In each scope, a name can only represent one entity. So, there cannot be two variables with the same name in the same scope. Using namespaces, we can create two variables or member functions having the same name.
// Here we can see that more than one variables
// are being used without reporting any error.
// That is because they are declared in the
// different namespaces and scopes.
#include <iostream>
using namespace std;
// Variable created inside namespace
namespace first
{
    int val = 500;
}
// Global variable
int val = 100;
int main()
{
    // Local variable
    int val = 200;
    // These variables can be accessed from
    // outside the namespace using the scope
    // operator ::
    cout << first::val << '\n';
    return 0;
}Output:
500

Definition and Creation:

Namespaces allow us to group named entities that otherwise would have global scope into narrower scopes, giving them namespace scope. This allows organizing the elements of programs into different logical scopes referred to by names.
  • Namespace is a feature added in C++ and not present in C.
  • A namespace is a declarative region that provides a scope to the identifiers (names of the types, function, variables etc) inside it.
  • Multiple namespace blocks with the same name are allowed. All declarations within those blocks are declared in the named scope.
A namespace definition begins with the keyword namespace followed by the namespace name as follows:
namespace namespace_name 
{
   int x, y; // code declarations where 
             // x and y are declared in 
             // namespace_name's scope
}
  • Namespace declarations appear only at global scope.
  • Namespace declarations can be nested within another namespace.
  • Namespace declarations don’t have access specifiers. (Public or private)
  • No need to give semicolon after the closing brace of definition of namespace.
  • We can split the definition of namespace over several units.
// Creating namespaces
#include <iostream>
using namespace std;
namespace ns1
{
    int value()    { return 5; }
}
namespace ns2
{
    const double x = 100;
    double value() {  return 2*x; }
}
int main()
{
    // Access value function within ns1
    cout << ns1::value() << '\n';
    // Access value function within ns2
    cout << ns2::value() << '\n';
    // Access variable x directly
    cout << ns2::x << '\n';      
    return 0;
}Output:
5
200
100

Classes and Namespace:

Following is a simple way to create classes in a name space
// A C++ program to demonstrate use of class
// in a namespace
#include <iostream>
using namespace std;
namespace ns
{
    // A Class in a namespace
    class geek
    {
    public:
        void display()
        {
            cout << "ns::geek::display()\n";
        }
    };
}
int main()
{
    // Creating Object of student Class
    ns::geek obj;
    obj.display();
    return 0;
}Output:
ns::geek::display()
Class can also be declared inside namespace and defined outside namespace using the following syntax
// A C++ program to demonstrate use of class
// in a namespace
#include <iostream>
using namespace std;
namespace ns
{
    // Only declaring class here
    class geek;
}
// Defining class outside
class ns::geek
{
public:
    void display()
    {
        cout << "ns::geek::display()\n";
    }
};
int main()
{
    //Creating Object of student Class
    ns::geek obj;
    obj.display();
    return 0;
}Output:
ns::geek::display()
We can define methods also outside the namespace. Following is an example code.
// A C++ code to demonstrate that we can define
// methods outside namespace.
#include <iostream>
using namespace std;
// Creating a namespace
namespace ns
{
    void display();
    class geek
    {
    public:
       void display();
    };
}
// Defining methods of namespace
void ns::geek::display()
{
    cout << "ns::geek::display()\n";
}
void ns::display()
{
    cout << "ns::display()\n";
}
// Driver code
int main()
{
    ns::geek obj;
    ns::display();
    obj.display();
    return 0;
}Output:
ns::display()
ns::geek::display()

Extending namespace and Unnamed namespace

It is also possible to create more than one namespaces in the global space. This can be done in two ways.
  • namespaces having different names
    // A C++ program to show more than one namespaces
    // with different names.
    #include <iostream>
    using namespace std;
    // first name space
    namespace first
    {
       int func() {  return 5; }
    }
    // second name space
    namespace second
    {
       int func() { return 10; }
    }
    int main()
    {
       // member function of namespace
       // accessed using scope resolution operator
       cout << first::func() <<"\n";       
       cout << second::func() <<"\n";
       return 0;
    }Output:
    5
    10
    
  • Extending namespaces (Using same name twice)
    It is also possible to create two namespace blocks having the same name. The second namespace block is nothing but actually the continuation of the first namespace. In simpler words, we can say that both the namespaces are not different but actually the same, which are being defined in parts.
    // C++ program to demonstrate namespace exntension
    #include <iostream>
    using namespace std;
    // first name space
    namespace first
    {
       int val1 = 500; 
    }
    // rest part of the first namespace
    namespace  first
    {
       int val2 = 501; 
    }
    int main()
    {
       cout << first::val1 <<"\n";       
       cout << first::val2 <<"\n";
       return 0;
    }Output:
    500
    501
    
Unnamed Namespaces
  • They are directly usable in the same program and are used for declaring unique identifiers.
  • In unnamed namespaces, name of the namespace in not mentioned in the declaration of namespace.
  • The name of the namespace is uniquely generated by the compiler.
  • The unnamed namespaces you have created will only be accessible within the file you created it in.
  • Unnamed namespaces are the replacement for the static declaration of variables.
// C++ program to demonstrate working of unnamed
// namespaces
#include <iostream>
using namespace std;
// unnamed namespace declaration
namespace
{
   int rel = 300;
}
int main()
{
   cout << rel << "\n"; // prints 300
   return 0;
}Output:
300


2 comments:


  1. Thank you for sharing such great information very useful to us.
    Embedded System Training in Delhi

    ReplyDelete
  2. Mobile app development company in Qatar with best prices

    Mobile app development company in Qatar brings you a heavy bill for your work but there is a company that gives you the best prices for your work. Primetech solutions are the cheapest Mobile app development company in Qatar. Being affordable doesn’t mean that they give you average services, being affordable means, they value everyone and are ready to give you the services at a genuine price. They are not developing only to earn money. Mobile app development is their passion. The team is very skilled and there are many things to show that too, firstly their customer's feedback tells you everything about them and then their work. You just look at their work and you will fall in love with their work.


    Mobile App Development Company in Qatar ǀ Features and Benefits

    Primetech solutions bring you a lot of benefits like not exaggerating anything, they will tell you with genuine concern and not to have a client only. They might take time to complete your work but whatever they say to do they will complete it 100%. They use premium tools to develop mobile apps and have all the world-class coders. For android apps they use java and for ios, they use swift program. Both are premium languages that need super-fast computers. They manage to give you affordable services with such premium tools. So, we don’t think you should think twice even after reading this. Just call them now and starts your dream app developing with the best.

    These small efforts for client happiness make Primetech solutions the best mobile app development company in Qatar.
    Check out their portfolio here- Portfolio


    By comparing different companies, you could get the idea of prices but what about the quality of work? You should compare the quality too. It will give you a basic idea of how hood the prices are but if you are going for comparison the also check the quality of service the cheapest company provide.

    The major part of Primetech Solutions is mobile app development, they believe in becoming the best at what they do. And that is why we conclude that Primetech Solution is the best app development in Qatar.

    Some other very beneficial services from Primetech Solutions.

    WEBSITE DEVELOPMENT

    • Website Design

    WEB HOSTING

    • Cloud Hosting

    DIGITAL MARKETING

    • Social Media Marketing
    • Search Engine Marketing
    • Brand Promotion



    What about the completion time of this company?

    Many of you people are wondering that what about the time limit, did they complete the projects in time? Yes, they do they might ask you for too much time but they will deliver the project on time. They do so because they want your app to be perfect and that is why they take this much time.

    Primetech Solutions is the best mobile app development company in Qatar as it delivers you the project in time, provides you the best which could be possible and many other things make this company the best. They are punctual as they work with such dedication that they always completed the projects on time, if you find any difficulty with the app after the delivery, they will help you with that too.


    Conclusion

    If you are searching for a genuine company for your app then this is your best shot. This company has everything which you will need they perform some excellent tasks and only they can do that. As we all know that application development is risky the client likes the project sometimes, they don’t but this company always bounces back with a better update if the client doesn’t like the app.

    If you want changes in the app during the development you could ask them without any hesitation and you will defiantly love their work.
    Just reach out to them and tell them about your needs and they will fulfil them.


    ReplyDelete

commnet here