If several problems have solutions that are generally similar, but differ in some details, a good technique to use is "indirection". That is, in the places where the details differ, replace those details by an indirect reference to the details.
The consequence is that much (maybe almost all) of the code doesn't have to change when applied to each of the problems.
How can indirect references be coded in C++?
Answer: By using a pointer type.