Back to slide -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'                namespaces/namespace-1-compilable.cc - Compilable variant of the program from above.Lecture 3 - slide 22 : 27
Program 2

// Reproduced from page 847 of 'The C++ Programming Language' (3ed).
// Shows 'using directives' and 'using declarations' by examples.  This variant is compilable.

#include <iostream>
#include <string>

namespace X {
  int i = 10, j = 11, k = 12;
}

int k;                // Global variable

void f1(){            // Illustrates using directives
  int i = 0;
  using namespace X;  // A using directive makes i, j, and k from X accessible
  i++;                // local i
  j++;                // X::j

  ::k++;              // global k
  X::k++;             // X::k
}

void f2(){            // Illustrates using declarations
  int i = 0;

  using X::j;       
  using X::k;         // hides global k

  i++;                // local i
  j++;                // X::j
  k++;                // X::k
}

int main(){
  f1();
  f2();
}