I've been trying to figure out the following problem without success:

I want to implement a general purpose code that operates on a base class, but when actually used, operates on derived class objects of the base class.

Specifically, I have a parent class that defines a broad category of numbers.
For sake of simplicity, I will call the class "number".
Let's say that I have several derived classes like "complex" , "rational", "integer".

I want the program to calculate a formula using the class number using member functions such as "add" "subtract" "multiply"...but I want to be able to swap in specific derived classes of "number" in the actual computation.

static void complex :: subtract (complex &diff, complex & a, complex & b) {
diff.real = a.real - b.real;
diff.imaginary = a.imaginary - b.imaginary;
}
static void rational :: subtract (rational &diff, rational &a, rational &b) {
// ... find common denominator
// ... subtract
// (actual code abbreviated)
}
void number :: absoluteDifference (number &result, number &a, number & b) {
number temp; // how do I initialize this to be the right kind of derived class?

number :: subtract (temp, a, b);
if (number :: isNegative (temp)) { // determines if parameter temp is negative
number :: negate (temp); // negates value in temp
}
number :: copy (result, temp); // copies value in temp to result
}

So how do I initialize temp to be the right kind of derived class of class number?
Ok, so obviously, in the above example, I could just directly use the result object as the temporary variable and eliminate the problem. In reality, the program I want to write is much more complex and temporary workspace objects are essential.

I would appreciate any help. Thanks.