Why do people use namespace std for iostream functions if it's included in the header?
Why do people use namespace std for iostream functions if it's included in the header?
Last edited by blankstare77; 07-23-2005 at 04:30 PM.
That sentance made no sense what so ever.
The namespace std includes most of your standard C++ I/O functions, and there 'using namespace std;' is typed early on in a program. This is of course down to personal taste - some prefer to use std::cout etc to show cout is a member of the std namespace.
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
A namespace is a way of grouping names together so if two different things in a program have the same name the compiler will be able to tell them apart. All of the names in the standard library are part of the std namespace. So if you want to use cout from <iostream>, you have to tell the compiler that you want std::cout. Another way to do this that some people use is by putting a using directive in their code to tell the compiler to look in a particular namespace for the name they use. So if you type using namespace std; and then later use cout, the compiler will check in namespace std for something called cout and will be able to find it.
Pre-standard headers (like <iostream.h>) don't have the namespace protection, which is why you can find old code without the std parts. However, the benefits of using the new standard library far outweigh the cost of typing a few extra characters, so everybody should be using the new headers (<iostream>) with the std namespace.
Here's an example of using the namespace:
The same example without using the namespace:Code://Division Program //An example of using namespace #include <iostream> using namespace std; //if you include this, you DONT have to put std:: before cout,cin, and endl int main() { int num1, num2, quotient; cout << "Enter your first whole number:"; cin >> num1; cout << "Enter your second whole number:"; cin >> num2; quotient=num1/num2; cout << "Your quotient is " << quotient << endl; cin.ignore(); cin.get(); return 0; }
Code://Division Program #include <iostream> //Since there is no namespace, you must put std:: before cout & cin, and endl int main() { int num1, num2, quotient; std::cout << "Enter your first whole number:"; std::cin >> num1; std::cout << "Enter your second whole number:"; std::cin >> num2; quotient=num1/num2; std::cout << "Your quotient is " << quotient << std::endl; std::cin.ignore(); std::cin.get(); return 0; }
Videogame Memories!
A site dedicated to keeping videogame memories alive!
http://www.videogamememories.com/
Share your experiences with us now!
"We will game forever!"
Uhh here's sample code that works fine in Visual C++ 6.
Code:<iostream.h> void main() { cout << "Hi this is just a dummy program" << endl; }
#include <iostream.h> -> Wrong! iostream.h is a deprecated header and is no longer standard.
void main() -> Very wrong! void main has never ever been standard, main must always return an int!
STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.
No longer "standard"? I use it and it's fine. I see it all the time on websites.
I used void because in those parameters it doesn't return a value. It just prints things on the screen (cout).
You use it now but when you move to a newer compiler that is more standard compliant all your code is wrong and you need to rewrite it because you are too lazy to follow the current standard. And those websites that uses those are old, outdated and in general not good learning resources.
And no no no no no no no NO NOOOOO!!!!!!!!! main always alwayas ALWAYS returns an int!!!!!
Last edited by Shakti; 07-23-2005 at 04:48 PM.
STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.
Uhh, on my compiler it produces errors.
Also, printing on the screen isn't considered as returning a value.
But ffs!!! this is perfectly valid C++ code:
and so is this:Code:#include <iostream> int main() { std::cout << "Some people just dont get it...main always returns an int!" << std::endl; }
Ok listen to me..if you are so stupid that you dont follow my corrections even after posting about it several times. especially since I have been here for almost 2 years now then I wont tell you any more! Just dont post any source here where you use void main because that will be the first thing people tell you until you change it! And why bother posting here if you cant take advices from those who knows better?Code:#include <iostream> int main() { std::cout << "Again some people just dont get it...main must always return an int!" << std::endl; return 0; }
Last edited by Shakti; 07-23-2005 at 04:57 PM.
STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.
Are there new features i'm missing that are in new compilers?
Well apart from your compiler being one of the worst when it comes to standard compliance nowadays it also has a crappy and buggy implementation of STL and C++ string class.
STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.
Where'd you get your compiler? How much?
How about this?
http://www.newegg.com/Product/Produc...82E16832102272
Actually you dont have to buy a compiler, a compiler that is considered to be one of the best out there is MingW, do a search for Bloodshed DevC++ on google and you will get a very good IDE together with a very good compiler.
STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.
What about Microsoft Foundation Classes?Originally Posted by Shakti