|
-
Feb 18th, 2003, 08:13 AM
#1
Thread Starter
Hyperactive Member
Newb Question: iostream and namespace std
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.
-
Feb 18th, 2003, 08:50 AM
#2
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 20th, 2003, 07:07 AM
#3
Thread Starter
Hyperactive Member
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 )?
-
Feb 20th, 2003, 06:56 PM
#4
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 21st, 2003, 01:49 AM
#5
Thread Starter
Hyperactive Member
-
Feb 21st, 2003, 05:54 AM
#6
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
Code:
/* 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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 21st, 2003, 07:39 PM
#7
Thread Starter
Hyperactive Member
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).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|