Container for abstract class objects

Hello,
I would appreciate some tips regarding a design of class hierarchies and abstract classes.

I want to manage a set of functions. Therefore I have an abstract class

1
2
3
4
5
class Function {
public: 
   /** Returns the value of the function at position x */
   virtual double value(double x) const = 0;
};


One could think of several classes inheriting this class, e.g.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Polynom: public Function {
public:
   double value(double x) {
      // calculate and return sum over i of a[i]*x^i
   };
private:
   double *a; // coefficients of the polynom
};

class expFunction: public Function {
public:
   double value(double x) {
      // calculate and return a * exp(b*x)
   };
private:
   double a,b;
};


Next thing I need is a kind of container class for Function objects. This is kind of difficult as Function is an abstract class and it is not possible to create Function objects.

The class I want to write should be like a vector of functions, but it should be possible to interpolate functions to get a function at 0.5 for example. Should I write a class that contains something like std::vector<*Function>
And if I do, how can I write an interpolation method?

Thanks for some hints!
Yes it would be best to use std::vector<Function*>
How do you interpolate functions? http://en.wikipedia.org/wiki/Interpolation
Take a look at the Boost Function library. http://www.boost.org/doc/libs/1_45_0/doc/html/function.html

It replaces inheritance with type erasure. This is a simplifying concept when dealing with function objects (functors and functions).

Topic archived. No new replies allowed.