namespace in C++

 c++ tutorials |  Admin

For example:

namespace mycode {

     float pi = 3.1415;

    

     int factorial(int number)

     {

           if (number)

                return factorial(number - 1) * number;

           else

                return 1;

     }

}

 

int factorial(int number)

{

     int ret=1;

     for (int i = 1; i <= number; i++)

           ret = ret * i;

     return ret;

}

 

#include <iostream>

int main()

{

     std::cout << "factorial of 7 using recursive method = " << mycode::factorial(7) << "\n";

     std::cout << "area of circle of radius 1 is " << mycode::pi * 1 * 1 << "\n";

 

     std::cout << "factorial of  7 using for loop = " << factorial(7) << "\n";

     return 0;

}

 

Here we defined the namespace named as mycode and inside of it we defined a float variable pi and a function factorial and used these in the main function. A same named function of factorial is defined outside the namespace and we can easily call this without any name collision.

There is not any feature of namespace in C.

Multiple namespace with same name can be used in the code, all will follow same namespace and all declaration of identifiers can be used with the same name. For example:

namespace const_pi {

     float pi = 3.14156;

     float multiply2(float n) { return 2 * n; }

}

 

namespace  const_pi {

     float pi_2 = multiply2(pi);

}

 

namespace const_pi {

     float pi_4 = multiply2(pi_2);

}

 

#include <iostream>

int main()

{

     std::cout << "value of pi = " << const_pi::pi << "\n" ;

     std::cout << "value of 2*pi = " << const_pi::pi_2 << "\n";

     std::cout << "value of 4*pi = " << const_pi::pi_4 << "\n";

     return 0;

}

 

namespace can be defined only at global scope. (We cannot define a namespace in function/class etc.

Nested namespace are allowed. For example:

namespace n1 {

     int value = 1;

     namespace n2 {

           int value = 2;

           namespace n3 {

                int value = 3;

                namespace n4 {

                      int value = n1::value * n2::value * n3::value;

                }

           }

     }

}

 

#include <iostream>

int main()

{

     std::cout << "value in the namespace n4 is " << n1::n2::n3::n4::value;

     return 0;

}


using namespace:

See below 2 examples:

//Example1

#include <iostream>

int main()

{

     std::cout << "we are using std namespace without \"using\" keyword :)";

     return 0;

}

 

//Example2

#include <iostream>

using namespace std;

int main()

{

     cout << "we are using std namespace with \"using\" keyword :)";

     return 0;

}

Note: The definition of std namespace is in the header file <iostream>

Both example compile and execute without any problem.

Now see below examples:

//Example1

#include <iostream>

#define print(str) std::cout << str << "\n"

 

namespace ns1 {

     int value = 1;

}

 

namespace ns2 {

     int value = 2;

}

 

int main()

{

     print(ns1::value);

     print(ns2::value);

 

     return 0;

}

 

//Example2

#include <iostream>

#define print(str) std::cout << str << "\n"

 

namespace ns1 {

     int value = 1;

}

 

namespace ns2 {

     int value = 2;

}

 

using namespace ns1;

using namespace ns2;

 

int main()

{

     print(ns1::value);

     print(ns2::value);

     return 0;

}

 

//Example3

#include <iostream>

#define print(str) std::cout << str << "\n"

 

namespace ns1 {

     int value = 1;

}

 

namespace ns2 {

     int value = 2;

}

 

using namespace ns1;

using namespace ns2;

 

int main()

{

     print(value);

     return 0;

}

 

The Example1 and Example2 compile and execute without any problem, but Eample3 will not compile and show error as "value" is ambiguous”.

 

Here the compiler cannot identify “value” from which namespace, which is causing problem to identify which namespace is used.