Directives and Declarations

I am not understanding the very first program example in every book. My original question was "Why are we not using #include for every instance when we want to use the standard libraries".

Upon digging (including through this website, I am finding that it has to do with Directives and Declarations. How come that some of the libraries are declared and for others we need to specify as directives?

In essence, my questions are:

1. Why and when I need to direct a program to use a library item?
2. Why and when I need to declare that the program should use a library item?
3. Are there any other methods that I should know about in order to use code from the standard libraries?

And just to be on the same page: A standard library is a place where pieces of reusable code reside. Am I correct?

Thanks in advance.

Best

Boris

1
2
3
4
5
6
7
8
  #include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World! ";
  cout << "I'm a C++ program";
}
I would suggest reading http://www.cplusplus.com/forum/articles/10627/ Disch does a wonderful job at articulating most of those articles/tutorials.

Maybe someone else can go more in depth, but I'll try to answer that:

A standard library is a place where pieces of reusable code reside.

That's correct in a general sense, yes.
If you're talking about C++, there's only one C++ standard library. A single header file (such as iostream) is not a "library" in this sense, but it's part of C++ standard library.
Headers that have 'c' at the beginning of them (ex: cmath) are the C++ versions of C's standard library headers (ex: math.h).

1. The #include directive in its simplest sense just copies the text from that file and pastes it where you put the #include statement.

http://www.cplusplus.com/reference/ This page shows a bunch of standard header files for the C++ language. You #include the right ones if you want to use the functions/classes/objects declared in those headers. For example, if you want to use std::vectors, then be sure to #include <vector> before you declare any in your code.

2. You should put #include statements at a global scope, usually at the top of the file you're working with (after header guards though, if you're working in a header file).
Thank you for your reply.

I read the article and was able to grasp the basic concepts. This also led me to the realization that I did not ask the correct question.

I was referring to "#include <iostream>" and "using namespace std".

If they (iostream and namespace std) are files/code part of the Standard Library, why one is using directive such as, "include" and the other is being declared as in "using". Can't I just write #include namespace std? Or vice versa "using iostream"?

Please, see my original questions (copied below) with regards my clarification:

1. Why and when I need to direct a program to use a library item?
2. Why and when I need to declare that the program should use a library item?
3. Are there any other methods that I should know about in order to use code from the standard libraries?

And also add a fourth one:

4. If a text refers to "C++ Standard Libraries", they really mean C++ Standard Library" which is a repository of reusable code. Am I correct?

Thanks in advance

Best

Boris
Thanks for the reply.

I need someone to dumb it down to my level. Please, keep in mind that I have no prior knowledge of programming and am at page #1 of the book, trying to decipher the first two rows of this program:

#include <iostream>
using namespace std;

int main ()
{
cout << "Hello World! ";
cout << "I'm a C++ program";
}

I am failing to understand why two pieces of reusable code stored in the same physical location are called in two different ways (please, also see my specific questions with regards that). Sorry, the examples in the links are too advanced for me yet.
Last edited on
I am failing to understand why two pieces of reusable code
They are not "two pieces of reusable code". Only one piece is <iostream>. It actually contains code.

using namespace std; is a completely different thing and is not related to actually bringing code to your program (and it is not good thing to use). It is here to save you need to fully qualify names from standard library ( std::cout ) and introduce potential for name clashes.
Topic archived. No new replies allowed.