-
3 Files of OO Language
While doing some reading on C++ I noticed there is a way to create files in the true object oriented fashion. Tto create 3 seperate files, a specification file, an implementation file, and a client file. I have never learned to make 3 seperate files. I have always had everything in what I think to be 1. I use VS.net by the way for my programs. What does my book mean when using 3 seperate files? Do i have to merge these files together after creating them so they work in unison and can compile together? I appoligize if this sounds silly, I am just a bit confused after reading this section. Thanks
-
I thought there was a section in the FAQ about this, but skimming the FAQ I couldn't find it. So....
If is a common practice in OOP to put a class declaration in a .h file and the method definitions for the class in a .cpp file with the same name as the .h file. You need to link these files to the driver file containing main() (or some other entry point). IF you are using an IDE like VC6.0, .Net, BCB, or DevC++, you just have to list the .h file in the include list like this:
#include "myClass.h";
before calling main(). In the above example, myClass.h must be in the same directory as the file containing main(). If it isn't, then you need to type the complete file path between the double quotes, not just the file name.
The exception to this practice is if you are using templated classes, in which case you need to put the class declaration and function definitions all in the .h file.
-
elad, great, thanks for shedding some light, my next question is, do you know how would i go about actualyl creating a ".h" file or specification file in VS.net?
-
also I have a few classes/struct that I need to create in a header file or specification file, do they all have to be seperate header files? My code is below:
Code:
const int MAX = 10;
struct car{
char AorD;
char license[6];
time carTime; //this is struct Time - should it be in another .h ?
char AM_PM[2];
int no_moves;
};
struct Time{
int hours;
char : ;
int minutes;
};
-
If they are classes/structs with no associated functions, they do not need to be each in a separate header file. The purpose of having a header and cpp file for one class is because it causes linking problems. In your main cpp file, you need to include the class definition and prototypes by including the header file, so you can use them. The class cpp file also needs to include the prototypes for its own purposes. The big problems start to arise when you have classes using other classes using other classes. I think the problems are redefinition, but I'm not sure. So, to be safe with them, if you have a class with its own functions, keep the class definition and prototypes in the header file, and keep the functions inside the cpp file. It makes the code alot cleaner. (Of course, if you are making a templated class, then you have to have everything in one header file, as it doesn't actually generate code).
So your time and car structs/classes do not need to be separated, as they don't have their own functions.