C++ Resources

Updated on Nov 1  2020



C++OOPSplitting

Deborah R. Fowler



OOP Splitting Files for C++  

Posted on Oct 19  2019
Updated 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:

Let's revisit the example file for the structure below.
MISSING IMAGE
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"

MISSING IMAGE

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.

MISSING IMAGEMISSING IMAGE
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.

MISSING IMAGE

Next is polymorphism