The Math thread got me thinking about implementing a set of classes to model different kinds of functions. What I have now is a base class called "Function" and derived classes "Constant" (simple numbers) and "Monomial" (a number times "x" to a power).

I'm having problems with the polymorphism aspect of it. I want to declare certain virtual functions (for example, taking a derivative) in the Function class that I will define in every derived class so that I can call them on any object of tyep Function, but I can't define them in the base class because it will be different for each derived class. So I made them pure virtual. There are other functions, however, (like "solve"), which I want to define in Function because they will be the same for all derived classes as well. Is it not allowed to have both pure virtual functions and normal, defined functions in a base class? I get an error with Borland 5.5 saying "Class 'Function' may not contain pure functions" on the line where I declare the pure virtual function.

This is what I would like to be able to do, in the end:

The other problem (which is actually worse because I haven't thought of a workaround) is related to these. I have defined these pure virtual functions like derivative to return Function objects. In reality, they will always be some subclass of Function like Monomial. The compiler complains that it can't instantiate an object of type Function. I understand why, but how else can I define it? More importantly, when I call derivative() on a Monomial, it will return another Monomial, but I can't use it as such because the compiler thinks it's a Function. Do I have to use pointers to fix these problems? What about references? I tried ref's but I can't return a ref to a local object, and with pointers it doesn't make much sense to create the objects on the stack and expect the user to delete them.vector<Function> someFuncs; // (not sure if this would have to be a vector of pointers, but that shouldn't be related to the problem I'm having yet)

someFuncs.push_back( Monomial(3,5) );

someFuncs.push_back( Constant(12.2) );

someFuncs.push_back( Exponential(3) ); // although I haven't even defined this class yet

// :

// :

// :

for( vector<Function>::iterator i = someFuns.begin(); i <= someFuncs.end(); i++ ) {

std::cout << i.derivative() << "\n;

// The above line would call different versions of the function "derivative"

std::cout << i.solve(5,0);

// This one, however, would always call the same code

}

Any help would be appreciated...