1

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);
};
Neysha
  • 11
  • 4
  • 5
    Firstly `TCat` won't compile because the contract here is that you must implement `bool Eat(int)`, secondly if you define a new function which doesn't match the base class's pure virtual function signature then this is not overriding but overloading – EdChum Jan 10 '17 at 09:33
  • 1
    Google the Liskov substitution principle. Then examine your code and see why what you do fails to uphold it. – StoryTeller - Unslander Monica Jan 10 '17 at 09:34
  • 1
    @EdChum without an explicit `using` declaration, this is hiding, not overloading. – Quentin Jan 10 '17 at 09:35
  • TCat's override of Eat "hides" the virtual function Eat in IAnimal. – CashCow Jan 10 '17 at 09:40
  • be sure to use the `override` keyword for any overriding functions. It will tell you if you get the signature wrong. – Paul Rooney Jan 10 '17 at 09:40
  • @Quentin yes that's true, but it won't work as the OP is intending besides – EdChum Jan 10 '17 at 09:48

4 Answers4

2

No. Signature of a virtual method can't be altered. Not even with default parameters.
The only exception is the covariance, where the return type can be different.

With C++11, the rule of thumb is to put override specifier after the supposed virtual method (either pure or not). If it doesn't compile, then something is wrong. In your code example, following will fail to compile:

bool Eat(int _i_food, int _i_amount) override;  // error
Community
  • 1
  • 1
iammilind
  • 68,093
  • 33
  • 169
  • 336
0

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.

Community
  • 1
  • 1
nvoigt
  • 75,013
  • 26
  • 93
  • 142
0

You need to provide implementation all pure virtual functions. You can override the function Eat(int _i_food) but it won't be possible to invoke the overloaded version using pointer to base class.

pSoLT
  • 1,042
  • 9
  • 18
0

Your problem is that the Eat method in TCat is not an override of the method in its base class but an overload, and it "hides" the base class method.

See this question:

Why does a virtual function get hidden?

Community
  • 1
  • 1
CashCow
  • 30,981
  • 5
  • 61
  • 92