- Joined
- Mar 25, 2007
- Messages
- 126
- Reaction score
- 0
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.
#ifndef NAMEOFTHEFILE_H
#define NAMEOFTHEFILE_H
....
code
....
#endif
Or else you'll get errors when yourfile.h is attempted to be included several times.
Well for one, when you have a class in a separate file? I admit I'm not 100% confident about how they get processed, but it's good practice to say the very least.Yeah, its quite standard to include the define, but when do you get header files to be included multiple times? I just include everything I need in the top of main.cpp (or main.h if I have one).
Yeah, its quite standard to include the define, but when do you get header files to be included multiple times? I just include everything I need in the top of main.cpp (or main.h if I have one).
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.