there must be some way to have a user define the RightHandSide function in the main program, and then call the SolveEquation method to solve the ODE associated with that RightHandSide function.
It's still a bit unclear for me, what exactly you're asking for, but from your comments it sounds you want to have a user defined function executed within your abstract classes context. I think this is possible, but you'll need to change the interfaces a bit:
class AbstractODESolver
{
public:
AbstractODESolver();
// ...
void SolveEquation(std::string filename) {
// Have an abstract implementation of the algorithm involving
// a call to the RightHandSide() method.
}
virtual ~AbstractODESolver();
protected:
virtual double RightHandSide(double y, double t) = 0;
private:
double stepSize;
double initialTime;
double finalTime;
double initialValue;
};
class ForwardEulerSolver : public AbstractODESolver
{
public:
typedef double (*FnType)(AbstractODESolver*,double,double);
ForwardEulerSolver(FnType fn_) : fn(fn_) { assert(fn); }
virtual ~ForwardEulerSolver();
private:
virtual double RightHandSide(double y, double t)
{
return (*fn)(this,y,t);
}
FnType fn;
};
If you're working in a c++11 environment you can use an inline lambda function definition to get the necessary function pointer:
int main()
{
ForwardEulerSolver::FnType rightHandSide =
[](AbstractODESolver* solver, double y, double t) {
// Replace with reasonable user implementation
return 0.0;
};
ForwardEulerSolver fwEulerSolver(rightHandSide);
fwEulerSolver.SolveEquation("MyInput.txt");
}
For pre c++11 standard you can use a module private function definition to implement the wanted behavior
namespace {
double RightHandSide(AbstractODESolver* solver, double y, double t) {
// Replace with reasonable user implementation
return 0.0;
}
}
int main()
{
ForwardEulerSolver fwEulerSolver(&RightHandSide);
fwEulerSolver.SolveEquation("MyInput.txt");
}
This is more or less replicating, what's called the Template Method Pattern, but handling a special (edge) use case, to allow setting a user defined implementation function for a particular part of the algorithm.