I have an interface class – IAnimal and 2 derived classes – Tcat and Tdog. I want both Tcat and Tdog to inherit the Eat function, however, I want Tcat to be able to have 2 parameters and Tdog to have the parameters that it inherits. Is this possible?
/// Pure virtual - to ensure all inherited animals have eat and sleep
class IAnimal
{
public:
virtual ~IAnimal() {}
virtual bool Eat(int _i_food) = 0;
virtual bool Sleep(int _i_time) = 0;
};
class TCat : public IAnimal
{
public:
bool Eat(int _i_food, int _i_amount); // can we override interface pure virtual from inherited class?
bool Sleep(int _i_time);
};
class TDog : public IAnimal
{
public:
bool Eat(int _i_food);
bool Sleep(int _i_time);
};
No
This is not possible. And it would not make sense either, because if those functions have a different signature, you would not be able to call them through the interface. What should happen if you pass one parameter through the interface and the actual instance needs two?
What you can do is override the one that you have to and then add an additional method with two parameters. But you will not be able to call the new one through the interface.