I'm having trouble with encapsulating my C++ code. So far, I've created 7 files: person.h, person.cpp, city.h, city.cpp, country.h, country.cpp and a main.cpp. I understand that each class would go into its own .h file and I believe I did that much correctly. However, I am unable to determine what would go into the implementation files (the country.cpp, person.cpp, city.cpp files) as well as what would go into the main.cpp file. Would anything in the
int main() go into the implementation files or would they be included in the main.cpp file only? Thanks in advance!
It seems like you need to include all the header files into the main program.
Like..
#include "city.h" at the top. Same for the other two.
And for the implementation files, you need to also include the correct header file for them. Just the ones that relate to it. Like in city.cpp, you would
Hi sethman410, thanks for your reply! I have that done so far. I was just wondering as to which parts of the original code would go into the city.cpp/country.cpp/person.cpp file. Basically, which parts of the original code (in my original post) would go into the implementation files, which parts wouldn't, etc. Also, what would the main.cpp file consist of.
Would anything in the int main () that is associated with the city class, including the city1 variable, have to go into the city.cpp file? Thanks again.
Thanks Shodan Ho! Your answer was very helpful. Just one question though -- where would the variables Person person1 go? Would it be located in the implementation file or the main.cpp? Thanks again.
So there are two distinct things:
(1) The _definition_ of the class which defines the code and variables associated. This is generally in the .h and .cpp as discussed earlier.
This defines the size, interface, code, behavior of all members of this class. But generally no instances are defined.
(2) The _instantiation_ and use of variables of this class.
An example is:
Person person1, person2;
which defines two instances of Person. Each has the same variables: