Question about multiple classes
Hi, I've planned to create a math library, containing a lot of different classes, basically starting with the most basic classes as natural numbers or positive integers, then negative integers, and zero. Then integers, which is a superset of these three. Then non-integer rational numbers, which uses an integer as the numerator and a natural number as the denominator. Then rational numbers, which is a superset to integers and non-integer rational numbers. The classes for positive integers, zero, negative integers and for non-integer rational numbers will all be concrete (not abstract), while integers and rational numbers will both be abstract classes. Then it is also possible to add classes for more complicated objects, like various kinds of expressions for irrational numbers, complex numbers, vectors and matrices, etc. etc. Finally, I will also have an abstract superclass which will be inherited by all math classes. Ideally, an arbitrary math object will be an object of that class.
Now to the problem: Operations between different math objects, such as the plus operation, will have to use many different functions, maybe as many as one function for each combination of the class types for the two arguments. Say for example that I have 5 different kinds of math objects that are non-abstract; maybe I will need to have 5*5 = 25 versions of the plus operation. How can I implement the plus operation in the code; as a virtual function? What would it look like? Say I have two objects of the super class; how would I perform the operation?
My idea was to in some way give each concrete class a number, which would work as a key to a two-dimensional array (5*5 in size, if there is 5 concrete classes) containing a lot of function pointers to different versions of the plus operation. For example, if the objects a and b are of subclasses with numbers 2 and 3, the place in the array which holds the function pointer to the operation that should be used on a and b, will have the coordinates (2, 3). If the function pointer is NULL it means that the operation is not supported between those two subclasses. I just don't know how to give each class a unique number; it feels unnecessary to use a virtual function for that reason. Isn't it possible to have a virtual constant for a class, that is stored in the v-table?
Thanks in advance.