Home   |   About   |   Terms   |   Contact    
Read & Learn
 

What is Standard Library in C++?

C++

☯ All C++ Articles     ☯ All Articles


A new product.







Popular Google Pages:



This article is regarding Standard Library & Namespaces of C++.
Last updated on: .



◕ Let a simple C++ program:
#include <iostream>
using namespace std;
int main()
{
cout << "Sky is blue!" << endl;
return 0;
}



◕ Here the iostream file is a part of the standard C++ library.

Standard library is a set of pre-written codes in C++. These have been written to carry many common tasks to run the program. Such as:
mathematical calculation,
inputs and outputs of a program etc.

These pre-written codes are very large. As a result we have to give the particular file name which we want to include in our program.

What is Namespaces in C++?

What is Namespaces?

Let in the above program we use another variable with the same name iostream. As a result there will be name clashes of iostream.

The Namespace is a mechanism or a system in C++ to avoid problems that can arise when duplicate names are used in a program. Let explain this.

As mentioned above Standard Library has a long routines or pre-written codes. And as usual it has so many names. So it is quite possible that we might accidentally use the same name of the Standard Library files or operators in our program. So there might be name clashes. To avoid this duplicate situation of name, a mechanism is there in C++. The name of this mechanism is called Namespaces.



How Namespaces works?

The Namespaces does its work by associating a given Set of Names from the standard library. These sorts of Names are called Namespace Name.

So, every item in the Standard Library has its own Name, plus the Namespace Name.

For an example:
std::cout;
std::endl;

Here, the names cout and endl are there in the standard library. As a result their full names are std::cout and std::endl.

The two colons sign ( :: ) that separate the Namespace Name from the Name is called the Scope Resolution Operator ( SRO ). This Scope Resolution Operator ( SRO ) has also different functions in C++.



◕ Using the full names of the operators in our program again and again look so uncomfortable. That means if we use the full name std::endl again and again in our programs then it looks so ugly, confusing and time consuming. As a result there is a solution of this.

The solution is, just to declare once the full name bellow the line
#include <iostream> in our main program.

Example:
#include <iostream>
using namespace std;

Here with the declaration,
using namespace std; we tell the compiler that I am going to use the Names from the Namespace std without their Namespace Name or the full name.






Popular Google Pages:





Top of the page

Amazon & Flipkart Special Products

   


Top of the page