(7 pts) Question S: Classes with Inheritance and the Diamond Problem in this pro
ID: 3743844 • Letter: #
Question
(7 pts) Question S: Classes with Inheritance and the Diamond Problem in this problem, different kinds of bats forage for food. Objects in class A are fruiteaters and will eat fruit. Possible food items are found at random using the method find_food string find foodO Not Shown:The tostrean is included and we are using the std nanespace. nt n -HIDDEN a randon int , 1 or 2. string food-fruit". "blood", "decaying natter) return foodn); Class A are the fruiteaters class A f public: bool eat(string food)f if (foodfruit") coutec "I'n a fruiteater and I an eating some fruit!"Explanation / Answer
1. A a(); // construct an object of class A
2. foundFood = a.eat(food);
3. while (food != "fruit")
4.
class B : public A {
public: bool eat(string food) {
if(food == "blood") {
cout << "I'm a bloodeater and I am eating some blood!" << endl;
return true;
} else {
cout << "Yuk! Bloodeaters don't eat: " << food << endl;
return false;
}
}
};
5.
class C : public A {
};
6.
class D: public A, public B {
};
7. While creating class B, the keyword "virtual" is necessary in the headers. It should look something like:
class B : virtual public A {
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.