Computer Science Canada namespace std and <iostream.h> vs <iostream> |
Author: | Mazer [ Mon Mar 15, 2004 4:48 pm ] |
Post subject: | namespace std and <iostream.h> vs <iostream> |
Just wondering how everyone feels about using #include <iostream.h> in their programs instead of #include <iostream> But not just for iostream, for fstream and string and vector as well. I hear alot of talk about how you aren't supposed to include the .h at the end because those are old C headers, and the C++ headers (without .h in the name) are supposedly better. Of course, if I do use the headers without .h, it means I will need to use using namespace std, and as I've noticed, that can blow up my executable to at least 540kb (and all it does is load a 3D katana and spin it). I'm sure there was something else I wanted to say, but a friend of mine distracted me with some pictures of Carmen Electra and... Er, so, what do you guys think? Any comments about why it would be "bad practice" to utilize the "old" headers? What's your personal preference? |
Author: | AsianSensation [ Mon Mar 15, 2004 6:55 pm ] |
Post subject: | |
I use <iostream> and <string> and etc etc etc without the .h I dunno, I guess it's Mr.Mckenzie's fault telling us to use those. |
Author: | Tony [ Mon Mar 15, 2004 8:23 pm ] |
Post subject: | |
i use <iostream> cuz it's shorter and you don't really have to use namespace... you could use std::cout instead |
Author: | Mazer [ Mon Mar 15, 2004 8:29 pm ] |
Post subject: | |
Yeah, but would you do that for every time you want to use cout? Adding std:: in front does cut down the size of the executable (who would've thought that an std could be a good thing... naoki ) that you'd get by using the standard namespace, but if I have alot of stuff that is in the standard namespace, that gets kinda annoying. Right now I'm only using std:: for declaring vector class objects. Which is good because I only need to use it once each time. |
Author: | wtd [ Mon Mar 15, 2004 10:05 pm ] | ||
Post subject: | |||
I always use <iostream>, <fstream>, <string>, <vector>, <list>, <algorithm> and such. It's the way of the future. The headers that end in .h might be there for legacy support, but make no mistake, that's the only reason they're there. As for:
I don't mind using it quite as much in my main function, though I prefer not to. However, I generally prefer to never use it in a library, and I only ever use it inside a function, and never in the global namespace. |
Author: | rizzix [ Mon Mar 15, 2004 11:28 pm ] |
Post subject: | |
meh... no opinion |
Author: | Tony [ Tue Mar 16, 2004 12:44 am ] |
Post subject: | |
I think that you're a tool, mazer. Suicide is something that I'd consider if I were you. The slow and painful kind. C++ is for losers, turing all the way. By the way, this is martin...err 'Darkness' Love it or shove it. |
Author: | wtd [ Tue Mar 16, 2004 12:57 am ] |
Post subject: | |
C++ is painful, but it's incredibly powerful. "The more programming languages you know, the more you realize they all suck." |
Author: | Mazer [ Tue Mar 16, 2004 8:23 am ] |
Post subject: | |
tony wrote: I think that you're a tool, mazer. Suicide is something that I'd consider if I were you. The slow and painful kind. C++ is for losers, turing all the way.
By the way, this is martin...err 'Darkness' Love it or shove it. LM ass O. As soon as you suggested suicide I kinda figured it would be Darkness, of course when you called me a tool I thought you might me James. And by that I mean, suicide would be a very good idea for you. Besides, tony wouldn't insult me, we're on the same team fighting Dan Shadow to get back the book of typos. |
Author: | Tony [ Wed Mar 17, 2004 12:34 am ] |
Post subject: | |
Your mom's on the same team fighting Shadow Dan. On a trampoline. |
Author: | Mazer [ Wed Mar 17, 2004 8:14 am ] |
Post subject: | |
I hereby decree that the next post shall be directed at Martin. |
Author: | Tony [ Wed Mar 17, 2004 11:33 am ] |
Post subject: | |
Your mom puts the tramp into trampoline |
Author: | Thuged_Out_G [ Tue Mar 23, 2004 5:49 pm ] |
Post subject: | |
tony wrote: Your mom puts the tramp into trampoline
odd, but i thought i had heard a rumor about you being conceived on a trampoline jk lol |
Author: | jonos [ Tue Mar 23, 2004 9:00 pm ] |
Post subject: | |
wqow this is interesting. i use the .h just because the first tut i found had that... but my sams book (which is not the best) says that you don't have to have the .h because the compiler knows what you mean, which i think may be wrong, but sams has a lot of books. |
Author: | jonos [ Tue Mar 23, 2004 10:26 pm ] |
Post subject: | |
to all you advanced c++ers, would you recommend using: using namespace std; or by using: std:: cout << "hello people " << std::endl; becaue though it may take longer to type, ive read some things agaist "using namespace std;". what would be the best practice to get into? |
Author: | Catalyst [ Tue Mar 23, 2004 10:38 pm ] | ||
Post subject: | |||
personaly i do this
but that's just me |
Author: | jonos [ Tue Mar 23, 2004 10:44 pm ] | ||
Post subject: | |||
would i also have to do that for:
or does cout cover that? alos, do i have to do that for all the structures like if statements and loops? |
Author: | wtd [ Tue Mar 23, 2004 10:46 pm ] | ||
Post subject: | |||
jonos wrote: would i also have to do that for:
or does cout cover that? No, cout and cin are each single variables (objects). jonos wrote: also, do i have to do that for all the structures like if statements and loops?
No, those are part of the C++ language itself. One of the first things you should do is make sure you understand where the language stops and the vocabulary (libraries) begin. |
Author: | jonos [ Tue Mar 23, 2004 10:50 pm ] |
Post subject: | |
ive never thought of it that way, ive been doing c++ for probably less than a month pretty seriously, but i never thought of it that way... ive never seen any reference refering to libraries and the c++ language itself, and they don't offer c++ at my school, so maybe someone should do a tut on that |
Author: | wtd [ Tue Mar 23, 2004 11:13 pm ] | ||||
Post subject: | |||||
Well, types that are part of the language itself:
Of course, these types will also vary depending on what kind of machine you're programming for. There's lots of syntax that's part of the C++ language itself.
And there's much much more, but my fingers are about to wear out. |
Author: | Thuged_Out_G [ Thu Mar 25, 2004 5:19 pm ] |
Post subject: | |
if you wouldnt mind, could you please explain these few topics a little more, i dont quite understand them for loop, switch, some block, label, namespace. i know what a for loop is, i just didnt quite understand the syntax you showed for it . i have seen label before in turing, but never used it, or looked into to it to find out its use. namespace, all i know about that is "using namespace std;" lol thanks |
Author: | wtd [ Thu Mar 25, 2004 6:04 pm ] | ||||||||||||||||||||
Post subject: | |||||||||||||||||||||
Well, in any loop you have:
The for loop conveniently combines these into one piece of syntax:
A switch is essentially a way to streamline long if/else structures. In C/C++/Java/PHP it only works with integers (including characters), though. Essentially, a single variable or function call is input, and then multiple values are compared against it. So instead of:
I could write:
The break; is necessary because of the way switch statements behave. Once something matches, it will continue evaluatng all of the code below it until it either hits the end of the switch or a break;. If I didn't have break after "case 42:", all four functions would be run, rather than the one I wanted. This can come in handy though, for doing the same thing for multiple matches. Let's say I want to execute the something() function if i is 42 or i is 27.
Blocks of code are handy because they introduce a new "scope" that can have its own set of variables. Let's say for instance that I have a variable named couner already declared, but I don't need to use it for a small section of code. I do, however, want to use it for a loop.
The output of that will be:
This is because the first counter we declared wasn't altered by the code inside the block. Not necessarily a good practice, but handy to know it's possible. A label is a name given to a particular location in a program. For instance, an if statement might be written as:
If you do this, however, there are any number of people who will han you up by your toenails. It's that bad an idea. Namespaces, however, are not a bad idea. Let me take a page from another topic and suppose we want to write a Rock, Paper, Scissors game. There will be a number of functions involved. In PHP, which lacks namespaces, I started every function name with "rps_". But instead, how about I just goup them all in one "rps" namespace?
Now, to use this function, I an either use:
Or...
Now of course that latter example would look a lot better if you had a lot more calls to functions in therps namespace. Hope this has helped. |
Author: | jonos [ Thu Mar 25, 2004 8:35 pm ] |
Post subject: | |
man, that was really helpful, you should write a book, or at least a pdf on all this stuff. thats awesome. thanks a lot you really clarified a lot of things, even though i didn't request it i used it so i think thug_g should request a lot more things |