Hot to get correct output using virtual? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок

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;;; func (animal); func (cat);//need output I'm eating a rat. } void func(Animal xyz) {; }

18th May 2018, 1:51 AM
1 ответ
+ 2
You need to pass by reference, if you pass it by value, a copy of type "Animal" is generated
18th May 2018, 2:26 AM
Christopher Dorado
Christopher Dorado - avatar