So i've just recently started working with c++, and anyhow according to my teacher i have to include the string library (#include <string> ) if i am using string, and i have to use the math library (#include <math.h> )if i use complex math.
well the other day while i was doing a program for a contest
i didn't include the string library or the math library and the code still ran fine.
You have to include them.
A particular header might pull in other headers, but that's an internal detail and nothing you are allowed to rely on. It varies between standard library implementations and even different releases of the same implementation.
#include <iostream>
# include <math.h>
# include <string>
# include <stdio.h>
# include <stdlib.h>
# include <MALLOC.H>
# include <TIME.H>
The <...> part simply tells the compiler that that file can be found
where it's usually located on your hard-drive.
You can, actually save those files anywhere you wish, but then you need to say where they are. Compilers don't like to go looking for things. # include c:\root\stuff\headers\"LIMITS.H" ( or something like this)
If you add 10 or 20 or 40 header files in a bank of #include statements, then it won't hurt or break anything, but may (will) slow down the program from a little to a lot when you run it. -also increases the size. This was a problem when you had to get the program to fit on a 560K floppy disc , but if you have 7 of 3TB drives and 16gb ram, then it isn't so much of a problem. If your program is less than , say, 1000 lines then you're not likely to notice the difference in speed.
If you leave-out the Include header file, then it "May" work.
It may also work only on your computer and when you hand it over to a friend or teacher, it may, just as quickly, stop working !
(because it then can't find everything it needs to run.)
Recommendation: Print a copy of all the common FUNCTIONS and also a copy of all the common Header files (like math.h)
Keep them at-hand, tape them to the PC, try to memorize their names.
- paper is good because then you can make comments, such as *Use this one always, or * use these for sorting programs, etc.