Putting your classes into headers
So far, our classes have just been pasted before main()
. If you continue to program that way, your code will all be in one file and appear as one big disorganized mess.
Therefore, it is a good programming practice to organize your classes into separate files. This makes editing each class's code individually much easier when there are multiple classes inside the project.
Take class Mammal
and its derived classes from earlier. We will properly organize that example into separate files. Let's do it in steps:
- Create a new file in your C++ project called
Mammal.h
. Cut and paste the entireMammal
class into that file. Notice that since theMammal
class included the use ofcout
, we write a#include <iostream>
statement in that file as well. - Write a "
#include
Mammal.h
" statement at the top of yourSource.cpp
file.
An example of what this looks like is shown in the following screenshot:
What's happening here when the code is compiled is...