0

Hot to get correct output using virtual?

Is it any error? #include <iostream> using namespace std; class Animal { public: virtual void eat() { cout << "I'm eating generic food."; } }; class Cat : public Animal { public: void eat() { cout << "I'm eating a rat.\n"; } }; void func(Animal xyz); int main() { Animal animal; Cat cat; animal.eat(); cat.eat(); func (animal); func (cat);//need output I'm eating a rat. } void func(Animal xyz) { xyz.eat(); }

18th May 2018, 1:51 AM
freedom
1 Réponse
+ 2
You need to pass by reference, if you pass it by value, a copy of type "Animal" is generated https://code.sololearn.com/c302nL6F5irR/?ref=app
18th May 2018, 2:26 AM
Christopher Dorado
Christopher Dorado - avatar