Abstract operator+ problem

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 ? :)
Last edited on
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
1
2
Base* b = &my_child;
b->operator += (5);
...
What if you did polymorphism with references? You might write
1
2
Base& b = my_child;
b += 5;

right? Wouldn't that require the virtual-ness?
Oh, right..
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 +)
So to sum it up, you say I should just virtual the operator+ without abstracting (=0) it, and that my "professor" is a moron ? (you might be right)..
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?
He said the base class has to be abstract.
And the operator+ is the only one who "Changes" in the derived classes.

1+1 = operator+ has to be abstract..

Or maybe this just means the usage of "virtual" and NOT the usage of =0 .. hmmm
Neither makes sense.

Operator overloading and inheritance are a weird combination to begin with. You should verify your understanding with your professor.

1
2
3
4
5
6
7
8
9
struct Base {
    virtual Base operator+( const Base& rhs ) const = 0;
};

// implies that:

struct Derived : Base {
    virtual Derived operator+( const Base& rhs ) const { /* actual implementation */ }
};


but one would almost surely want to add two Derived's together, rather than a Base to a Derived. But adding two Derived's requires

 
    /* virtual */ Derived operator+( const Derived& rhs ) const { /* ... */ }


which would not satisfy the pure virtual in Base due to the difference in parameter type.

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.
Last edited on
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?
Last edited on
L B
Than we lose the polymorphism functionality , ....
What about a templated base class?

1
2
3
4
5
6
7
8
9
10
11
12
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);
}


Would the above work?
But we were taught operator+ should return by value..

It should work though
Topic archived. No new replies allowed.