I am currently writing a program that models various types of numbers and manages them polymorphically in a Set object (already written and tested). My inheritance relationship is this:
Multinumber (all virtual functions, virtual class)
inherited by Pairs, Complex, Rational
The subclasses all have mostly the same functions with different parameters.
The problem that I am running into is in functions like this:
Multinumber& Complex::operator=(const Multinumber &rhs)
{
imag=rhs.imag;
real=rhs.real;
return *this;
}
Because I am treating this polymorphically, my return types and parameters all have to be of type Multinumber, in order to override the base class’s parameters. However, I am having a terrible time getting this to compile. I am getting a boatload of errors along the lines of:
error: 'const class Multinumber' has no member named 'imag'
which is true. Multinumber does not have an imag attribute or function. But how can I get the compiler to realize that the Multinumber& rhs will always be Complex, or to treat it as such? Thank you for your help.
This is what my superclass looks like:
class Multinumber
{
public:
virtual Multinumber& operator+(Multinumber&);
virtual Multinumber& operator=(Multinumber&);
virtual bool operator==(Multinumber&);
virtual string object2string();
virtual Multinumber& makeobject(double, double)=0;
};
I think you’ll have to cast. Try this: