What you are doing is not normal, and (IMHO) wrong in your case. It might be possible to justify using inheritance that way, but there are better ways to it.
Generally, inheritance expresses an
is-a relationship. For example, you might derive a Car or a Bicycle class from a Vehicle class -- a car
is a vehicle and a bicycle
is a vehicle. In your case, is a cosine function a plot? That seems pretty weird.
Also, classes are usually used to represent things. You are using a class to represent a function. Now, that is not necessarily wrong, in fact it is used quite often (look up "functor"). But in your case, it is a little strange. Consider just making the functions functions instead of classes.
Here is what I suggest you do. Rather than deriving Cosine from FunctionPlot, do something like this:
class Function { ... virtual float operator()( float x ) = 0; ... }; class Cosine : public Function { ... float operator()( float x ) { return cos( x ); } ... }; class Plotter { ... void Plot( Function const & f, ... ) { ... y = f( x ); ... } ... }; int main( int argc, char *argv[] ) { Cosine c( ... ); Plotter p( ... ); p.Plot( c, ... ); }
In this example, the classes Function and Cosine are "functors", i.e. a class that acts as a function, using the () operator. Functors might be a little confusing, so you could write it like this instead:
class Function { ... virtual float Compute float x ) = 0; ... }; class Cosine : public Function { ... float Compute( float x ) { return cos( x ); } ... }; class Plotter { ... void Plot( Function const & f, ... ) { ... y = f.Compute( x ); ... } ... };
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!