Hello, my homework is to make 3 classes:
LongNumber - base
IntLoneNumber - derived (from base only)
FltLongNumber - derived (from base only)
So I made this chars array in LongNumber, everything works fine when I placed in each of the derived classes an operator+ , but I'm told to make LongNumber an abstract class with this operator+ , so when I virtual operator+ (..) = 0; I get huge errors..
I tried to change in the base class the return\get values of the operator+ , but nothing works, what should I do ? :)
Did you actually put a ".." in there?
I believe, you can have a virtual operator + (base&) in base class and then overload it as operator + (child&) in child class. The problem what to return still remains, as a + cannot return a reference.
You could define an operator += instead (which does need to return a reference) and then call it from a global operator + (it is more convenient to have + not as a member function, as this way implicit casting can happen on both sides of +)
But still, it makes little sense to make these operators virtual. It's not like you are going to write
hamsterman , but they force us to have an operator+ as an abstract function in the base class..
But I think the problem is what this operator+ returns, in base it returns "Base" , but in child it should return "Child" and I'm not sure but think this is the problem, but I can't fix it, please help :)
You're right, and there is no good way around it.. (not any that I can see, anyway) (bad ways would include using += instead or returning a pointer to an object dynamically allocated inside +)
No, it doesn't matter whether you add the =0 (I know that it compiles without it, but still, it's a different function). All overloads of a virtual function have to have the same argument types and the same return type.
I don't know about your professor. Did he explicitly state that your operator + has to be a member of the class and that it has to be virtual?
I think that the idea is to have the base decide on a common ground beforehand:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class LongNumber
{
//Decide on the most capable of all datatypes and use that one for this variable.
double _value;
//The following constructor is required by the operator+. Make it public if needed.
protected:
LongNumber(double initialValue) : _value(initialValue)
{ }
//Now operator+ can be implemented in the base class.
public:
LongNumber operator+(const LongNumber &op2)
{
return LongNumber(_value + op2._value);
}
};
I do realize that such an approach is not the best, but it at least allows the operator to be common to all children.
What if you defined the operator+ so that it is correct, and then overloaded it so that it would use the overloaded one? IE define the Base operator+(Base& rhs) and then overload Child operator+(Child& rhs) as a new function?
template<class T>
class LongNumber
{
protected:
//Or public. Whatever.
T GetValue();
public:
LongNumber<T>& operator+(const LongNumber<T> &op2);
//And another one in case we want to mix types:
template<class T, class U>
LongNumber<T>& operator+(const LongNumber<U> &op2);
}