C++ – Name Visibility – Namespace – Using

C++ – Name Visibility – Namespace – Using

Name Visibility

Named entities, such as variables, functions, and compound types need to be declared before being used in C++.

– An entity declared outside any block has global scope, meaning that its name is valid anywhere in the code.

– An entity declared within a block, such as a function or a selective statement, has block scope, and is only visible within the specific block in which it is declared.


int foo;        // global variable

int some_function ()
{
  int bar;      // local variable
  bar = 0;
}

int other_function ()
{
  foo = 1;  // ok: foo is a global variable
  bar = 2;  // wrong: bar is not visible from this function
}

Example:


// inner block scopes
#include <iostream>
using namespace std;

int main () {
  int x = 10;
  int y = 20;
  {
    int x;   // ok, inner scope.
    x = 50;  // sets value to inner x
    y = 50;  // sets value to (outer) y
    cout << "inner block:\n";
    cout << "x: " << x << '\n';
    cout << "y: " << y << '\n';
  }
  cout << "outer block:\n";
  cout << "x: " << x << '\n';
  cout << "y: " << y << '\n';
  return 0;
}

The result is:
inner block:
x: 50
y: 50
outer block:
x: 10
y: 50

Namespaces

Namespaces allow us to group named entities that otherwise would have global scope into narrower scopes, giving them namespace scope. This allows organizing the elements of programs into different logical scopes referred to by names.

The syntax is:

...
namespace myNamespace
{
  int a, b;
}
...
// Access at variables with:
myNamespace::a
myNamespace::b
...

Working example:


// namespaces
#include <iostream>
using namespace std;

namespace foo
{
  int value() { return 5; }
}

namespace bar
{
  const double pi = 3.1416;
  double value() { return 2*pi; }
}

int main () {
  cout << foo::value() << '\n';
  cout << bar::value() << '\n';
  cout << bar::pi << '\n';
  return 0;
}

The result is:
5
6.2832
3.1416

Using

The keyword using introduces a name into the current declarative region (such as a block), thus avoiding the need to qualify the name.


// using
#include <iostream>
using namespace std;

namespace first
{
  int x = 5;
  int y = 10;
}

namespace second
{
  double x = 3.1416;
  double y = 2.7183;
}

int main () {
  using first::x;
  using second::y;
  cout << x << '\n';
  cout << y << '\n';
  cout << first::y << '\n';
  cout << second::x << '\n';
  return 0;
}

The result is:
5
2.7183
10
3.1416

The keyword using can also be used as a directive to introduce an entire namespace:


// using
#include <iostream>
using namespace std;

namespace first
{
  int x = 5;
  int y = 10;
}

namespace second
{
  double x = 3.1416;
  double y = 2.7183;
}

int main () {
  using namespace first;
  cout << x << '\n';
  cout << y << '\n';
  cout << second::x << '\n';
  cout << second::y << '\n';
  return 0;
}

The result is:
5
10
3.1416
2.7183

‘using’ and ‘using namespace’ have validity only in the same block in which they are stated or in the entire source code file if they are used directly in the global scope. For example, it would be possible to first use the objects of one namespace and then those of another one by splitting the code in different blocks:


// using namespace example
#include <iostream>
using namespace std;

namespace first
{
  int x = 5;
}

namespace second
{
  double x = 3.1416;
}

int main () {
  {
    using namespace first;
    cout << x << '\n';
  }
  {
    using namespace second;
    cout << x << '\n';
  }
  return 0;
}

The result is:
5
3.1416

Reference: http://www.cplusplus.com/doc/tutorial/namespaces/

By |C++, Video Games Development|Commenti disabilitati su C++ – Name Visibility – Namespace – Using

C++ – Functions

C++ – Functions

Functions allow to structure programs in segments of code to perform individual tasks.

Functions with type

Working samples:


// function example
#include <iostream>
using namespace std;

// integer - name of the function - data
int addition (int a, int b)
{
  int r;
  r=a+b;
  return r;
}

int main ()
{
  int z;
  z = addition (5,3);
  cout << "The result is " << z; // the result is 8
}


// function example
#include <iostream>
using namespace std;

int subtraction (int a, int b)
{
  int r;
  r=a-b;
  return r;
}

int main ()
{
  int x=5, y=3, z;
  z = subtraction (7,2);
  cout << "The first result is " << z << '\n';
  cout << "The second result is " << subtraction (7,2) << '\n';
  cout << "The third result is " << subtraction (x,y) << '\n';
  z= 4 + subtraction (x,y);
  cout << "The fourth result is " << z << '\n';
}

Functions with no type – void


// void function example
#include <iostream>
using namespace std;

void printmessage ()
{
  cout << "I'm a function!"; // it prints 'I'm a function!'
}

int main ()
{
  printmessage ();
}

Arguments passed by reference

// passing parameters by reference
#include <iostream>
using namespace std;

void duplicate (int& a, int& b, int& c)
{
  a*=2;
  b*=2;
  c*=2;
}

int main ()
{
  int x=1, y=3, z=7;
  duplicate (x, y, z);
  cout << "x=" << x << ", y=" << y << ", z=" << z;
  return 0;
}

The result:
x=2, y=6, z=14

By |C++, Video Games Development|Commenti disabilitati su C++ – Functions

C++ – For Statement

C++ – For Statement

Working samples:


// countdown using a for loop
#include <iostream>
using namespace std;

int main ()
{
  for (int n=10; n>0; n--) {
    cout << n << ", ";
  }
  cout << "liftoff!\n";
}

The result:

10, 9, 8, 7, 6, 5, 4, 3, 2, 1, liftoff!


// break loop example
#include <iostream>
using namespace std;

int main ()
{
  for (int n=10; n>0; n--)
  {
    cout << n << ", ";
    if (n==3)
    {
      cout << "countdown aborted!";
      break;
    }
  }
}

The result:
10, 9, 8, 7, 6, 5, 4, 3, countdown aborted!


// continue loop example
#include <iostream>
using namespace std;

int main ()
{
  for (int n=10; n>0; n--) {
    if (n==5) continue;
    cout << n << ", ";
  }
  cout << "liftoff!\n";
}

The result:
10, 9, 8, 7, 6, 4, 3, 2, 1, liftoff!

By |C++, Video Games Development|Commenti disabilitati su C++ – For Statement

C++ – Do While Statement

C++ – Do While Statement

Working sample:


// custom countdown using while
#include <iostream>
using namespace std;

int main ()
{
  int n = 10;

  while (n>0) {
    cout << n << ", ";
    --n;
  }

  cout << "liftoff!\n";
}

The result:

10, 9, 8, 7, 6, 5, 4, 3, 2, 1, liftoff!


// echo machine
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str;
  do {
    cout << "Enter text: ";
    getline (cin,str);
    cout << "You entered: " << str << '\n';
  } while (str != "goodbye");
}

The result:

Enter text: hello
You entered: hello
Enter text: who’s there?
You entered: who’s there?
Enter text: goodbye
You entered: goodbye
Process returned 0

By |C++, Video Games Development|Commenti disabilitati su C++ – Do While Statement

C++ – Switch Statement

C++ – Switch Statement

Working samples:

...
switch (expression)
{
  case constant1:
     group-of-statements-1;
     break;
  case constant2:
     group-of-statements-2;
     break;
  .
  .
  .
  default:
     default-group-of-statements
}
...
By |C++, Video Games Development|Commenti disabilitati su C++ – Switch Statement