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 Laughing and you don't really have to use namespace... you could use std::cout instead Laughing

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 Wink ) 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:

code:
using namespace std;


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. Wink
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 Wink Laughing

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

code:

using std::cout;
using std::endl;


but that's just me

Author:  jonos [ Tue Mar 23, 2004 10:44 pm ]
Post subject: 

would i also have to do that for:

code:

using std::cin;


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:

code:

using std::cin;


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 Confused

Author:  wtd [ Tue Mar 23, 2004 11:13 pm ]
Post subject: 

Well, types that are part of the language itself:

code:
int // usually 32-bit
char // usually 8-bit
short // usually 16-bit
long // usually 32-bit
long long // usually 64-bit
float // 32-bit floating point number
double // 64-bit floating point number
long double // sometimes available, 80-bit floating point


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.

code:
// For loop
for ( ... ; ... ; ... ) { ... }

// do while loop (for looping at least once)
do { ... } while ( ... );

// While loop
while ( ... ) { ... }

// conditional
if ( ... ) { ... }
else if ( ... ) { ... }
else { ... }

// "Switch" statement
switch ( ... ) {
   case 1:
      ...
   case 2:
      ...
   default:
      ...
}

// break out of the current block
break;

// some block
{ ... }

// function
int some_function( ... ) { ... ; return ... ; }

// procedure
void some_proc( ... ) { ... }

// Go somewhere
goto LABEL;

// a label
LABEL:

// create a new name for a type
typedef new_type old_type;

// a struct (class with all public members)
struct [optional name]
{
   ...
};

// a class
class some_class
{
   private:
      ...
   protected:
      ...
   public:
      ...
};

// a namespace
namespace some_namespace
{
   ...
}

// getting at something in a namespace
some_namespace::something

// declaring a pointer
some_type * ptr;
some_type *ptr;
some_type* ptr;

// declaring a reference
some_type & ref;
some_type &ref;
some_type& ref;

// dereferencing a pointer
some_type value = *ptr;

// getting a pointer to a variable
some_type * ptr = &value;


And there's much much more, but my fingers are about to wear out. Smile

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 Confused. 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 Laughing

thanks

Author:  wtd [ Thu Mar 25, 2004 6:04 pm ]
Post subject: 

Well, in any loop you have:

  • Initialization (of variables).
  • Test
  • Update (variables)


The for loop conveniently combines these into one piece of syntax:

code:
for ( initilization ; test ; update ) { ... }


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:

code:
int i = 42;
if (i == 42)
   something();
else if (i == 54)
   something_else();
else if (i == 12)
   something_entirely_different();
else
   why_bother();


I could write:

code:
int i = 42;
switch (i) {
   case 42:
      something();
      break;
   case 54:
      something_else();
      break;
   case 12:
      something_entirely_different();
      break;
   default:
      why_bother();
}


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.

code:
int i = 42;
switch (i) {
   case 42: case 27:
      something();
      break;
   case 54:
      something_else();
      break;
   case 12:
      something_entirely_different();
      break;
   default:
      why_bother();
}


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.

code:
#include <iostream>

int main()
{
   int counter = 32;

   {
      for (int counter = 0; counter < 3; counter++)
         std::cout << counter << std::endl;
   }

   std::cout << counter << std::endl;
}


The output of that will be:

code:
0
1
2
32


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:

code:
#include <iostream>

int main()
{
      int i = 42;
      if (i == 42) goto IF42;
      goto NOT42;
   IF42:
      std::cout << "Ok" << std::endl;
      goto END;
   NOT42:
      std::cout << "Doh!" << std::endl;
   END:
      return 0;
}


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?

code:
namespace rps {
   std::string get_user_input() {
      // ... do something here
   }
}


Now, to use this function, I an either use:

code:
int main()
{
   std::string user_input = rps::get_user_input();
}


Or...

code:
int main()
{
   using namespace rps;
   std::string user_input = get_user_input();
}


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 Very Happy


: