C++: Namespace

This entry is part 4 of 61 in the series C++

A namespace is a declarative region that attaches an identifier to any names declared inside it.

The identifier makes it less likely that a name will conflict with names declared elsewhere in the program.

In the example below, std is a namespace declaration.

#include <iostream>

int main()
{
	std::cout << "Hello World";
	getchar();
}

By using std::cout, we ensure that we are using cout in the namespace std and not elsewhere.

To make our code look cleaner, we can use using namespace.

#include <iostream>
using namespace std;

int main()
{
	cout << "Hello World";
	getchar();
}

 

Series Navigation<< C++: The main function
C++: Addition of 2 numbers >>

Leave a comment

Leave a Reply