Activity 9-2 - Namespaces and Using Directives



In some of the programs that we have written so far we have included the using namespace std, which means that we will be using all the std namespace in those programs.  The std namespace contains all the names defined in the standard library files (such as iostream).  To explain this better let's look at the following example.  When we include:
#include<iostream>

In your program, this statement will place all the name definitions (cin, cout, ...) into the std namespace.  If we don't have the using namespace std in the program, then we can define some of the names such as cin or cout to have different meaning other than their standard meanings.  The standard definition of these names are supposed to be in the std namespace, but if we don't include that with a using directive, then our program does not know about it.

We can use more than one name space at the same time.  We are always in a namespace called the global name space and often we use the std namespace.  If a name is defined in both name spaces, we will get an error.  Thus, if a name is defined in two namespaces, we can use the namespaces one at a time in the same program at two different places.

For example, in the following code segment, the function my_function is defined in both namespaces name_space1 and name_space2 in two different ways.

{
      using namespace name_space1;
      my_function( );
}
{
      using namespace name_space2;
      my_function( );
}

Note that the first namespace and my_function are in one block while the second namespace and my_function are in another block.  Thus, the first namespace is valid in the first block and the second one is valid in the second block.  The first my_function uses the definition given in name_space1 and the second one uses the definition given in the name_space2.

Creating a Namespace
To place some program in a namespace, simply include the program in the namespace grouping:

namespace Name_Space_Name
{
      The_Program
}

To make a program defined in this namespace available to your programs, use using directive:

using namespace Name_Space_Name;

To illustrate this let's look at the following program.

// P92.cpp - This program illustrates the use of namespaces
#include<iostream>
using namespace std;

namespace cm
{
       double area(double length, double width);
}

namespace meter
{
       double area(double length, double width);
}

double area_km(double length, double width);

int main ( )
{
      double length, width;  // dimension of a rectangle
      double A;  // area of a rectangle

       cout << "Enter the length and the width of the rectangle. \n";
       cout << "Assuming unit is meter \n";
       cin >> length >> width;

       {
                using namespace cm;
                A = area(length, width);
                cout << "Area is: " << A << endl;
        }

        {
                using namespace meter;
                A = area(length, width);
                cout << "Area is: " << A << endl;
         }

         A = area_km(length, width);
         cout << "Area is: " << A << endl;

         return 0;
}

namespace cm
{
    double area(double length, double width)
    {
              cout << "From namespace cm, I am sending area in cm^2 back\n";
              return (length*100)*(width*100);
     }
}

namespace meter
{
    double area(double length, double width)
    {
              cout << "From namespace meter, I am sending area in m^2 back\n";
              return length*width;
     }
}

double area_km(double length, double width)
{
              cout << "From std namespace,  I am sending area in km^2 back\n";
              return (length/1000)*(width/1000);
 }

In the above program we have asked the user to input the length and width of a rectangle in meters.  Then, we have used different name spaces to compute the area of THE rectangle using three different units.  As you have noticed, the program that computes the area in namespace cm and meter are both called area, but they perform different computations depending on their definitions in their respective namespace.

Exercise 9.2
A student has found a summer job in a painting company.  He is to estimate the amount of paint that the company needs to paint rectangular fields of different sizes.  On average,

            a) 0.2 kg (kilogram) of paint is needed to paint a one square meter area (0.2 kg/m2).
            b) 22 mg (milligram) of paint is needed to paint a one square centimetres area (22 mg/cm2).
            c) 19.6 T (Ton) of paint is needed to paint a one square km area (19.6 T/km2).

Use the above program to estimate the amount of paint required to paint a rectangle with the length and width that the user will enter from the keyboard.  You need to compute the amount of paint in three different units (kg, mg, and Ton).