Hi. i have learned how to write classes. My question is, how can i write my Class on another .cpp file and include its functions in the main.cpp?
Printable View
Hi. i have learned how to write classes. My question is, how can i write my Class on another .cpp file and include its functions in the main.cpp?
Declare the class in a header (.h) file, define its functions in class.cpp, then #include this .h file both in your class.cpp and main.cpp.
Don't forget to (in .h files)
Or else you'll get errors when yourfile.h is attempted to be included several times.Code:#ifndef NAMEOFTHEFILE_H
#define NAMEOFTHEFILE_H
....
code
....
#endif
The reason you want to put the check is because, let's say several files include your header file. If there is no check, then if those other files are ever used together, your header file will be included multiple times.
For example, let's say <fstream.h> requires <stdio.h> (I dunno if it does, I'm just making it up), so it includes it, and let's say <iostream.h> also uses <stdio.h>, so it includes it. Now let's say you write a program that uses both <iostream.h> and <fstream.h>. If <stdio.h> didn't have the check, you would end up including it 2 times.
Well, the problem is that <iostream> won't compile on it's own then if it uses functions from <stdio> and didn't include it, and same goes for <fstream>.
But even ignoring that fact for a moment, if you want to use any file, you shouldn't be required to know every single file that must be included for the file you actually want to work. And from a practical, as well as abstracted, point of view, it's not your job to know that.
Let's say you want to use a completed 3d game engine written by someone else, that uses a lot of includes. The writers of the game engine will put all the includes necessary in their code, and for simplicity, all you would have to do is include the game engine files, instead of dig into their code to find all all the files they used. Let's say you later decided you didn't want the audio portion of their engine, you'd then need to go through all their code again and figure out which functions they use for the sound, make sure other parts of their code don't use those functions too, and only then would you know what is safe to have removed from your includes. As you can see, it becomes very impractical.
Another consideration is that you wrote a file that you didn't intend to be used elsewhere, so you left the includes in. Then someone else wanted to use your code, and included your file. Point is, it is confusing and an enormous waste of time to remove includes from all other files and place them only in your main file or the one you're working on, and then have to remove those includes and replace them in another file when you try to modify that other file. Then when you want to modify the library code, you'll need to place back the includes in the library and make sure all the other libraries don't include them, etc etc. Much easier with a simple define/ifndef.