Why should we use #include
To bring the standard library's I/O functionality into our program.
while we are using using namespace std?
This allows us to use that functionality without writing std:: each time we do.
This is unrelated to the previous step. Writing only using namespace std does not bring I/O functionality into your program, and writing only #include does not allow us to use that functionality without writing its components' names out in full (including the std:: prefix).
- The
#include directive determines what we can use;
- The
using namespace declaration determines how we can use it.
Perfectly fine:
#include
int main()
{
std::cout << "Hello world!\n";
}
Also valid:
#include
using namespace std;
int main()
{
cout << "Hello world!\n";
}
Not valid:
int main()
{
std::cout << "Hello world!\n";
}
And neither is this:
using namespace std;
int main()
{
std::cout << "Hello world!\n";
}
or this:
using namespace std;
int main()
{
cout << "Hello world!\n";
}