Hi I'm trying to fully understand inheritance before I start using abstract classes / virtual methods.
My question is: Is there a way of re-using a function (in this case an operator overload) of the base class, which return a base class object, in the derived class?
The example is the following: Reptile is a public derived class of Creature. Creatures have a Position object (x,y) as member data. When I add a creature and a position, the new creature has the old position summed to the added position.
Creature Creature::operator+(const Position& source) const
{
Creature copy{*this};
return copy += source; // += has the specific implementation of the summation
}
Main:
Position pos{};
Reptile rep{};
Reptile rep2 = rep + pos; // ERROR: No viable conversion from 'Creature' to 'Reptile'
I understand why it is not working, technically, but isn't the whole point of inheritance that I can reuse functionalities of the base class, specially when it's dealing with variables defined in the base class?
Thank you