little confusion with srand, rand and time functions

Hi,

I am just starting out with c++ and while trying to do a little exercise about flipping a coin and what not I was reading about those functions.

As far as i understood i would have to include: <stdlib.h> for srand and rand, and <time.h> for time.

Oddly enough while playing a bit with the code i noticed that even if i only include <iostream> my code works.

The questions are:
does iostream inherits functions from stdlib and time?
if yes are other standard libraries included in iostream?

is there any reason why is it like this? I mean, why should i use <stdlib.h> if i can always include <iostream>.

Thanks in advance!
You can't rely on that. The implementors of your standard library might have decided to have iostream include those headers (likely only indirectly) for practical purposes (because they needed a function from one of these headers), but this might change in the very next update of your standard library and is likely completely different in other implementations.
Those inclusions are not guaranteed.
You should include everything that you expect to use. And it is <cstdlib> and <ctime> for c++
Note that stdlib.h and time.h are the depreciated (in C++) names for the headers. You should use cstdlib and ctime if you want them to be included.

does iostream inherits functions from stdlib and time?

Probably. But it's not required, so you might find somewhere else that it isn't the case.

if yes are other standard libraries included in iostream?

Same as above.

Usually because you might as well use what is already written. Why rewrite timing functions if they are already written and work perfectly well?

As for why you should include them, read above.
Thank you very much for the replies...
it makes sense now.

So in short, I should stick to "ctime" and "cstdlib" names and dont rely that much on "iostream".

are there functions on iostream that are expected to be there? i.e. functions that cannot change in other implementations or even on new updates of IDE for example.
iostream is generally included because you need to include it to use std::cin and std::cout.
Topic archived. No new replies allowed.