-
July 3rd, 2008, 05:11 AM
#1
what is diff between #include<iostream> and using namespace std
Hi everyone
I am new to c++. In the program why we need both
#include<iostream> and using namespace std statements.
what is the difference between these two.
are cin,endl and cout.. are methods ?
-
July 3rd, 2008, 05:17 AM
#2
Re: what is diff between #include<iostream> and using namespace std
I suggest that you read a good beginner's book on C++. I recommend Accelerated C++ if you have a programming background or are a fast learner.
-
July 3rd, 2008, 09:39 AM
#3
Re: what is diff between #include<iostream> and using namespace std
#include <iostream> gives you access to std::cin, std::cout, std::endl, etc.
using namespace std; allows you to drop the prefix in your code. You could instead have written
using std::cout;
using std::cin;
using std::endl;
if you preferred.
-
July 3rd, 2008, 11:37 AM
#4
Re: what is diff between #include<iostream> and using namespace std
Yes, so in short, #include <iostream> loads in certain features from another file so you can use them in your program. Using namespace std allows you to access the members of the std namespace without qualifying them. You'll know what all that means when you learn about namespaces, but basically this allows you to say:
cout << "Hello, World!"; whereas you'd normally have to say:
std::cout << "Hello, World!";
Its just so you don't have to type as much.
Last edited by Mikau; July 3rd, 2008 at 11:45 AM.
-
July 3rd, 2008, 05:07 PM
#5
Re: what is diff between #include<iostream> and using namespace std
#include takes the contents of a header file and places where the #include was. (Technically it doesn't have to be a file but in practice it is.) The declarations for different input/output stream objects and classes are in iostream, so when you #include it, you're putting those declarations at the top of your file. When you use those objects, like cout and cin, the compiler needs to know what they are and what they can do. That information is found in the iostream file that you included.
A namespace is just a way of grouping names so that two things with the same name can be used without conflict. All names in the standard library are in the std namespace. This helps avoid problems when you use names or other code you use has names that are the same as the fairly common names in the standard library.
So when you use a name from the standard library, you have to tell the compiler that it is in the std namespace. One way to do it (and my preference) is to use the std:: prefix. If you aren't worried about naming conflicts, you can use a using directive to avoid having to type std:: for any name in the std namespace. That is what using namespace std is. It tells the compiler that if it is trying to find what a particular name refers to, it should also check the std namespace to see if it is in there.
So when you type std::cout you're telling the compiler you want the object called cout in the std namespace. Or if you use the using namespace std line, then when you type cout you are telling the compiler that you want something named cout, which could be in the std namespace. The compiler then looks for the declaration and/or definition of cout, and because you included the information in iostream, the compiler finds the cout there, recognizes that it is the one you want, and uses it.
Originally Posted by Indianblues
are cin,endl and cout.. are methods ?
No. cout and cin are instances of a class (like global variables). I forget what endl is, but I believe it is an instance as well.
Last edited by jlou; July 3rd, 2008 at 05:13 PM.
-
July 4th, 2008, 01:14 AM
#6
Re: what is diff between #include<iostream> and using namespace std
I forget what endl is, but I believe it is an instance as well.
The standard manipulators, including std::endl, are functions.
Incidentally, are you guys really going to explain every single language feature in detail when there are good books out there that do the same thing, but with a more structured approach to learning?
-
July 4th, 2008, 09:57 AM
#7
Re: what is diff between #include<iostream> and using namespace std
The distinction I explained was one I wondered about myself when I was first learning. That's why I figured it was worth the paragraph.
But one does have to draw the line somewhere.
-
July 4th, 2008, 07:05 PM
#8
Re: what is diff between #include<iostream> and using namespace std
Originally Posted by laserlight
The standard manipulators, including std::endl, are functions.
Incidentally, are you guys really going to explain every single language feature in detail when there are good books out there that do the same thing, but with a more structured approach to learning?
Not everybody is learning from a book. Not every book teaches all concepts in a way that all readers understand.
I actually would prefer formats like this for clarifying understanding because the nature of a forum allows small misunderstandings to be corrected more quickly.
Besides, you can't get mad at me, I haven't even posted in weeks.
P.S. Thanks for the endl clarification.
-
July 4th, 2008, 08:03 PM
#9
Re: what is diff between #include<iostream> and using namespace std
Originally Posted by jlou
I actually would prefer formats like this for clarifying understanding because the nature of a forum allows small misunderstandings to be corrected more quickly.
There is a limit to what is covered in a forum, and what really is covered in great detail in practically all C++ books.
Also, if the coder is not using a book or books, they are seriously hampering their likelihood of learning C++ properly.
Regards,
Paul McKenzie
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
|