A namespace
namespace MyNameSpace {
int start;
int end;
class counter {
int count;
public:
counter(int n) {
count = n;
}
void reset(int n) {
count = n;
}
};
}
A nested namespace
#include <iostream>
using namespace std;
namespace MyNamespace1 {
int i;
namespace MyNamespace2 { // a nested namespace
int j;
}
}
int main()
{
MyNamespace1::i = 19;
MyNamespace1::MyNamespace2::j = 10; // this is right
cout << MyNamespace1::i << " "<< MyNamespace1::MyNamespace2::j << "\n";
// use MyNamespace1
using namespace MyNamespace1;
cout << i * MyNamespace2::j;
return 0;
}
19 10
190
function in a namespace reference variables in the same namespace
#include <iostream>
using namespace std;
int integer1 = 98;
namespace Example
{
const double PI = 3.14159;
void printValues();
namespace Inner
{
enum Years { FISCAL1 = 1990, FISCAL2, a };
}
}
namespace
{
double doubleInUnnamed = 88.22;
}
int main()
{
Example::printValues();
return 0;
}
void Example::printValues()
{
cout << integer1 << "\n(global) integer1 = " << ::integer1
<< "\nIinteger1 = " << Inner::a << endl;
}
98
(global) integer1 = 98
Iinteger1 = 1992
Reference global variable, variable in the nested namespace
#include <iostream>
using namespace std;
int integer1 = 98;
namespace
{
double doubleInUnnamed = 88.22;
}
namespace Example
{
const double PI = 3.14159;
void printValues();
int integer1 = 100;
namespace Inner
{
int integer1 = 99;
}
}
int main()
{
cout << "doubleInUnnamed = " << doubleInUnnamed;
cout << "\n(global) integer1 = " << integer1;
cout << Example::integer1 << "\nIinteger1 = "
<< Example::Inner::integer1 << endl;
return 0;
}
doubleInUnnamed = 88.22
(global) integer1 = 98100
Iinteger1 = 99
Reference variables in namespaces
#include <iostream.h>
namespace first
{
int var = 5;
}
namespace second
{
double var = 3.1416;
}
int main () {
cout << first::var << endl;
cout << second::var << endl;
return 0;
}
5
3.1416
Reuse namespace
#include <iostream>
using namespace std;
namespace MyNamespace {
int i;
}
namespace MyNamespace {
int j;
}
int main()
{
MyNamespace::i = MyNamespace::j = 10;
// refer to MyNamespace specifically
cout << MyNamespace::i * MyNamespace::j << "\n";
// use MyNamespace namespace
using namespace MyNamespace;
cout << i * j;
return 0;
}
100
100
Use a namespace
#include <iostream>
using namespace std;
namespace MyNameSpace {
int start;
int end;
class counter {
int count;
public:
counter(int n) {
if(n <= start) count = n;
else count = start;
}
void reset(int n) {
if(n <= start) count = n;
}
int run() {
if(count > end) return count--;
else return end;
}
};
}
int main()
{
MyNameSpace::start = 100;
MyNameSpace::end = 0;
MyNameSpace::counter ob1(10);
int i;
do {
i = ob1.run();
cout << i << " ";
} while(i > MyNameSpace::end);
cout << endl;
MyNameSpace::counter ob2(20);
do {
i = ob2.run();
cout << i << " ";
} while(i > MyNameSpace::end);
cout << endl;
ob2.reset(100);
MyNameSpace::end = 90;
do {
i = ob2.run();
cout << i << " ";
} while(i > MyNameSpace::end);
return 0;
}
10 9 8 7 6 5 4 3 2 1 0
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
100 99 98 97 96 95 94 93 92 91 90
Use function defined in a namespace
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream>
#include <cstdlib>
namespace MyLib {
double readAndProcessSum (std::istream&);
}
int main()
{
using namespace std;
double sum;
try {
sum = MyLib::readAndProcessSum(cin);
}
catch (const ios::failure& error) {
cerr << "I/O exception: " << error.what() << endl;
return EXIT_FAILURE;
}
catch (const exception& error) {
cerr << "standard exception: " << error.what() << endl;
return EXIT_FAILURE;
}
catch (...) {
cerr << "unknown exception" << endl;
return EXIT_FAILURE;
}
// print sum
cout << "sum: " << sum << endl;
}
#include <istream>
namespace MyLib {
double readAndProcessSum (std::istream& strm)
{
double value, sum;
/* while stream is OK
* - read value and add it to sum
*/
sum = 0;
while (strm >> value) {
sum += value;
}
if (!strm.eof()) {
throw std::ios::failure
("input error in readAndProcessSum()");
}
// return sum
return sum;
}
}
a
I/O exception: input error in readAndProcessSum()
Use Namespace to organize functions
#include <ios>
#include <iostream>
#include <ostream>
void f(int i)
{
std::cout << "int: " << i << "\n";
}
namespace MyNamespace {
void f(double d)
{
std::cout << "double: " << std::showpoint << d << "\n";
}
void call_f()
{
// finds MyNamespace::f(double) first.
f(3);
}
}
int main()
{
MyNamespace::call_f();
using MyNamespace::f;
using ::f;
f(4);
}
double: 3.00000
int: 4
Using namespace aliases
#include <iostream>
#include <ostream>
namespace original {
int f();
}
namespace ns = original;
int ns::f() { return 42; }
using ns::f;
int g() { return f(); }
int main()
{
std::cout << ns::f() << "\n";
std::cout << original::f() << "\n";
std::cout << g() << "\n";
}
42
42
42
using namespace in program block
#include <iostream.h>
namespace first
{
int var = 5;
}
namespace second
{
double var = 3.1416;
}
int main () {
{
using namespace first;
cout << var << endl;
}
{
using namespace second;
cout << var << endl;
}
return 0;
}
5
3.1416
using namespace std;
#include <iostream>
using namespace std;
int main()
{
// Print hello world on the screen
cout << "Hello World";
return 0;
}
Hello World"
Using the using directive
#include <iostream>
#include <ostream>
using namespace std;
namespace A {
int x = 1;
}
namespace B {
int y = 2;
}
namespace C {
int z = 3;
using namespace B;
}
namespace D {
int z = 4;
using namespace B;
int y = 5;
}
int main()
{
int x = 60;
using namespace A;
using namespace C;
cout << x << "\n";
cout << y << "\n";
cout << C::y << "\n";
cout << D::y << "\n";
}
60
2
2
5
Using unnamed namespaces.
#include <iostream>
#include <ostream>
namespace {
int i = 10;
}
namespace {
int j; // same unnamed namespace
namespace X {
int i = 20;
}
namespace Y = X;
int f() { return i; }
}
int main()
{
std::cout << ::f() << "\n";
std::cout << Y::i << "\n";
std::cout << f() << "\n";
}
10
20
10