PDA

Click to See Complete Forum and Search --> : Newb Question: iostream and namespace std


Comreak
Feb 18th, 2003, 07:13 AM
Why do you need both? I know this is a real newb question, but it's really been bothering me lately. What's the difference, and why do I need both to use cin and cout? Thanks in advance.

CornedBee
Feb 18th, 2003, 07:50 AM
You mean why do you need both
#include <iostream>
and
using namespace std;
?

That's because they do different things. The first actually includes the definitions. If you have only the #include you already use cin and cout, but they are in the namespace std, so you have to refer to them by full name:
std::cout << "Hello, World!" << std::endl;

The using thing imports the symbols from iostream into the global namespace so that you don't need the std:: to reference them.

Comreak
Feb 20th, 2003, 06:07 AM
So 'using namespace std' just says that your going to be using stuff from std.

Another question, what are symbols (hey, I'm on a roll here :) )?

CornedBee
Feb 20th, 2003, 05:56 PM
So 'using namespace std' just says that your going to be using stuff from std.

Nearly. You can use stuff from std whether you write that or not. It tells the compiler that you won't explicitly say that the stuff you use is in std.

Another question, what are symbols?

Symbol is the common name for variable, constant, function, class etc. names.

Comreak
Feb 21st, 2003, 12:49 AM
So types = symbols?

CornedBee
Feb 21st, 2003, 04:54 AM
No, the type names are symbols.
There are 5 parts of code: keywords, operators, literals, symbols and comments.

Comments are lines that start with // or blocks enclosed by /* ... */.
Keywords are all reserved words: int, float, if, while, break, return, class...
Literals are explicit values: 4, 23.1, 0xe3, 'A', "Hello"
Operators are, well, operators: +, -, %, &, *, ...
Symbols are everything else. In

/* Define an array */
int my_array[12]; // Array is 12 elements large

int is a keyword, [] is an operator, 12 is a literal. /* ... */ and // ... are comments. my_array is a newly defined symbol. Whenever after it appears in the code the compiler knows that you are referring to that array.

Comreak
Feb 21st, 2003, 06:39 PM
Thanks for clearing the confusion up, Corned Bee. I always wondered what symbols where. They are mentioned alot in my compilers but nowhere in any of my books (yet).