Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C++ - NAMESPACES & USING


Namespace can be thought of as your personal space.


Say for example, you are working on a project. At the same time, you have a colleague who is also working on the same project.


And there is a scenario, where both of you have to create a class called Add.


But does C++ allows you to create same class within one project?


Well ! The answer is yes, but with Namespace.


Let us simplify with the below example.


Example :



#include <iostream>
using namespace std;

namespace John 
{
    class Add
    {

        public:
        int addition(int x, int y)
        {
    		int z = x + y;
    		return z;
        }
    };
}

namespace Paul 
{
    class Add
    {
        public: 
        int addition(int a, int b, int c)
        {
    		int z = a + b + c;
    		return z;
        }
    };
}

int main() {	

    John::Add add1;
    int result = add1.addition(5, 4);
    cout << "The addded result for John's namespace is : " << result << endl;
        
    Paul::Add add2;
    int value = add2.addition(6, 2, 3);
    cout << "The addded result for Paul's namespace is : " << value << endl;

	return 0;
}


Output :



  The addded result for John's namespace is : 9
  The addded result for Paul's namespace is : 11

So, in the above example, there are two guys, John and Paul working in the same file.


And both of them needs to have a class called Add.


So, they have their own namespaces.


Namespace for 'John'


Example :



	namespace John 
    {
        class Add
        {
    
            public:
            int addition(int x, int y)
            {
        		int z = x + y;
        		return z;
            }
        };
    }



Namespace for 'Paul'


Example :



	namespace Paul 
    {
        class Add
        {
            public: 
            int addition(int a, int b, int c)
            {
        		int z = a + b + c;
        		return z;
            }
        };
    }



Now, that both of them have their own namespace. How can they create their own objects of Add class?


In simple words, The namespace John has an Add class,


class Add
{
	public:
	int addition(int x, int y)
	{
		int z = x + y;
		return z;
	}
};

Also namespace Paul has an Add class,


class Add
{
	public:
	int addition(int a, int b, int c)
	{
		int z = a + b + c;
		return z;
	}
};

How would the Add object of John's namespace be different from Paul's namespace.


Well! The solution is using the namespace name itself.


And in the main method, we have invoked the Add class of John(i.e. namespace) using the :: sign.


John::Add add1;

i.e. Instead of creating the object of Add class directly,


Add add1;

We have created the object using the namespace name (i.e. John),


John::Add add1;

And used the add1 object to calculate the result.


int result = add1.addition(5, 4);
cout << "The addded result for John's namespace is : " << result << endl;

Similarly, we have created the Add object for Paul's namespace.


Paul::Add add2;
int value = add2.addition(6, 2, 3);
cout << "The addded result for Paul's namespace is : " << value << endl;

So, that was how you can create a namespace.


Now, let me tell you something interesting. So far, you were using namespace but unknowingly.


Guess where ?


Let's see below.


Writing namespace with 'using' keyword


Let's see the below example,


Example :



#include <iostream>
using namespace std;

int main() {
    cout << "Hello World";
}


Output :



  Hello World

Any guesses so far ?


Well ! the print statement itself contains the namespace.


cout << "Hello World";

std is the namespace that we have included with using keyword,


using namespace std;

What if we do not want to use the 'using' keyword?


Let us modify the above example,


Example :



#include <iostream>

int main() {
    std::cout << "Hello World";
}


Output :



  Hello World

So, in the above example all we have done is, we have included the namespace std with cout with ::(As we have done in the first example).


std::cout << "Hello World";

And we don't have to use the below line,


using namespace std;