I have a base class called animal, and a dog and a cat that inherit from Animal. And a multiinheritance class called dogcat, that inherit from dog and cat, in Animal i have method called sleep. When i want to use that method from dogcat, i get the error "DogCat::sleep" is ambiguous, i do understand the problem, but i read in a book that it should be possible, when you declare sleep as virtual - but it does not work.
Is this not possible is the book wrong or is there any workaround?
class Animal
{
public:
Animal(){}
virtual void sleep()
{
cout << "zzzzzzzzz" << endl;
}
virtual void eat() = 0;
};
class Dog: public Animal
{
protected:
Dog(){}
virtual void eat() override
{
cout << "eats dogfood" << endl;
}
};
class Cat :public Animal
{
public:
Cat(){}
virtual void eat() override
{
cout << "eats catfood" << endl;
}
};
class DogCat : public Dog, public Cat
{
public:
DogCat(){}
using Dog::eat;
};
int main(int argc, char** argv) {
DogCat *DC = new DogCat();
DC->sleep();//Error
}