Introduction to Programming Languages/Scoping with Namespaces

From Wikibooks, open books for an open world
< Introduction to Programming Languages
Jump to: navigation, search

Namespaces are named program regions used to limit the scope of variables inside the program. They are used in many programming languages to create a separate region for a group of variables, functions, classes, etc. The usage of namespaces helps to avoid conflict with the existing definitions. Namespaces provide a way of implementing information hidden. Some examples of namespaces in Java are classes and packages. Examples of namespaces in C++ are classes, namespaces, and struct. The source code bellow illustrates the usage of namespace in Java:

  1. import calc.math.*;
    
  2.  
    
  3. package geometry;
    
  4.  
    
  5. public class Circle {
    
  6.   private double radius;
    
  7.  
    
  8.   public Circle(double r) {
    
  9.     radius = r;
    
  10.   }
    
  11.  
    
  12.   public double getRadius() {
    
  13.     return radius;
    
  14.   }
    
  15.  
    
  16.   public double getArea() {
    
  17.     return calc.math.pi * radius * radius;
    
  18.   }
    
  19. }
    

The Java code above defines a namespace called geometry. This definition provides a way of discriminating the class Circle from others that could be defined by other programmers. This discrimination is performed because the defined namespace is incorporated to the class. So, the class becomes geometry.Circle. The first line of the code shows an import statement, which is the way Java shares definitions. It is interesting to note that the method code>getArea uses the definition pi from the imported namespace calc.math.

In C++ language, the namespace keyword is used to create namespaces. The include statement is a way of sharing definition in C++. The next example illustrates the definition of two namespaces (first and second) and their use. Namespaces are usable by taking their name reference with scope resolution operator (operator "::"). This is illustrated in the statement cout << first::var << endl; at line 14. Namespaces are also used with the using keyword, which makes all the members of the namespace available in the current program and the members can be used directly, without taking reference of the namespace. The statement present in the line 3 of the code makes the cout object available in the function main without the usage of the scope resolution operator.

  1. #include <iostream>
    
  2.  
    
  3. using namespace std;
    
  4.  
    
  5. namespace first {
    
  6.   int var = 5;
    
  7. }
    
  8.  
    
  9. namespace second {
    
  10.   double var = 3.1416;
    
  11. }
    
  12.  
    
  13. int main () {
    
  14.   cout << first::var << endl;
    
  15.   cout << second::var << endl;
    
  16.   return 0;
    
  17. }
    

The keywords private and public, which will be better detailed in other section of this wikibook, modify the visibility of the definitions in a namespace. There is no difference between a class and a struct in a C++ program, except that by default, the members of a struct are public, and the members of a class are private. Also structs are by default inherited publicly and classes by default are inherited privately. Other than that, whatever you can do in a class, you can do in a struct.

Language librariry is a collection of definitions and implementations that provide ready-to-use functions. They are a way to grow a programming language. In a C++ program, a library can be included using using the #include directive. The previous C++ example used this construct in the line 1 to include the iostream library.

Scoping with Blocks