Deborah R. Fowler
OOP Splitting Files
for C++
Posted on Oct 19 2019Updated Oct 20 2019
This section assumes you have read the basics of OOP for C++, pointers and inheritance.
SPLITTING FILES
Generally speaking, as your program become more and more complex, it is consider good programming style to split every class into its own set of files:
- class.h - definition
- class.cpp - functionality
If you are using Visual Studios, you can create .cpp and .h files and VS will take care of the bookkeeping. If you are using geany or another glorified editor, you will need to create a makefile.
Start by creating a Phone.h and Phone.cpp. The definition will go into the .h file and the functionality in the .cpp. They will be deleted from your main file and replaced with:
#include "Phone.h"
And in each .h and its corresponding .cpp is the previous code. For the full code download the zip here (set up is for Windows).
Try it from the original file and see how you do.
The makefile which is named Make is a special file that runs when you type make (in this case I am on Windows so mingw32-make is executed. On linux you would simply remove the -static-libstdc++ line. I have also added a very simply clean command so if you run mingw32-make clean it will delete .o files.
Next is polymorphism