How to create a namespace in C++
Posted on November 30, 2022 • 2 minutes • 399 words
In C++ programming, a namespace is a collection of names or identifiers related to one another.
Syntax:
namespace namespace_name {
// code here
}
Example:
namespace anooptube_namespace
{
int getName()
{
std::cout << "#anooptube" << std::endl;
}
}
How to create a arithmetic namespace
Step-1:
-
Create a
header file
named arithmetic.h -
Add header guards.
// arithmetic.h
#ifndef ANOOPTUBE_ARITHMETIC_H
#define ANOOPTUBE_ARITHMETIC_H
#endif
- Add namespace declaration.
// arithmetic.h
#ifndef ANOOPTUBE_ARITHMETIC_H
#define ANOOPTUBE_ARITHMETIC_H
namespace arithmetic
{
}
#endif
- Add required functions.
// arithmetic.h
#ifndef ANOOPTUBE_ARITHMETIC_H
#define ANOOPTUBE_ARITHMETIC_H
#include <iostream>
namespace arithmetic
{
int getSum(int x, int y)
{
std::cout << "Called arithmetic getSum()" << std::endl;
return x+y;
}
int getDiff(int x, int y)
{
std::cout << "Called arithmetic getSum()" << std::endl;
return x-y;
}
}
#endif
you have successfully added a header file with arithmetic namespace.
Step-2:
- Create a
cpp file
named main.cpp
// main.cpp
#include <iostream>
int main() {
std::cout << "Hello Welcome to Anooptube!\n";
}
- include arithmetic.h
// main.cpp
#include <iostream>
#include "arithmetic.h"
int main() {
std::cout << "Hello Welcome to Anooptube!\n";
}
- We must use scope-resolution operator :: to access functions in the arithmetic namespace.
// main.cpp
#include <iostream>
#include "arithmetic.h"
int main() {
std::cout << "Hello Welcome to Anooptube!\n";
std::cout << arithmetic::getSum(100,200) << std::endl;
std::cout << arithmetic::getDiff(200,50) << std::endl;
}
Nested namespaces
It is possible to add namespaces in another namespace. It is called nested namespaces.
We can add sum and diff namespaces in arithmetic
namespace.
// arithmetic.h
#ifndef ANOOPTUBE_ARITHMETIC_H
#define ANOOPTUBE_ARITHMETIC_H
#include <iostream>
namespace arithmetic
{
namespace sum
{
int getSum(int x, int y)
{
std::cout << "Called arithmetic getSum()" << std::endl;
return x+y;
}
}
namespace diff
{
int getDiff(int x, int y)
{
std::cout << "Called arithmetic getSum()" << std::endl;
return x-y;
}
}
}
#endif
Access of getSum
and getDiff
will be
// main.cpp
#include <iostream>
#include "arithmetic.h"
int main() {
std::cout << "Hello Welcome to Anooptube!\n";
std::cout << arithmetic::sum::getSum(100,200) << std::endl;
std::cout << arithmetic::diff::getDiff(200,50) << std::endl;
}
using keyword
we can use using
keyword to simplify the namespace access.
Syntax:
using namespace namespace_name;
Example 1:
// main.cpp
#include <iostream>
#include "arithmetic.h"
using namespace arithmetic;
int main() {
std::cout << "Hello Welcome to Anooptube!\n";
std::cout << sum::getSum(100,200) << std::endl;
std::cout << diff::getDiff(200,50) << std::endl;
}
Example 2:
// main.cpp
#include <iostream>
#include "arithmetic.h"
using namespace arithmetic::sum;
using namespace arithmetic::diff;
int main() {
std::cout << "Hello Welcome to Anooptube!\n";
std::cout << getSum(100,200) << std::endl;
std::cout << getDiff(200,50) << std::endl;
}