I have a base class called an animal, and a dog and cat that are inherited from Animal. And a multi-level class called dogcat, which is inherited from the dog and cat, in Animal I have a method called sleep. When I want to use this method with dogcat, I get the "DogCat :: sleep" error message ambiguously, I understand the problem, but I read in the book that it should be possible when you declare a dream as virtual - but this does not work.
Is it possible that the book is incorrect 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();
source share