Кто-нибудь может объяснить вывод следующего кода?
#include <iostream>
#include <string>
class Animal
{
public:
Animal(const std::string & name) : _name(name) { }
~Animal() { }
virtual void printMessage() const
{
std::cout << "Hello, I'm " << _name << std::endl;
}
private:
std::string _name;
// other operators and stuff
};
class Cow : public Animal
{
public:
Cow(const std::string & name) : Animal(name) { }
~Cow() { }
virtual void printMessage()
{
Animal::printMessage();
std::cout << "and moo " << std::endl;
}
};
int main() {
Cow cow("bill");
Animal * animal = &cow;
cow.printMessage();
animal->printMessage();
}
Вывод:
Здравствуйте, я Билл
и мычание
Здравствуйте, я счет
, не понимаю почему. Животное-указатель указывает на объект типа Cow. printMessage - это виртуальная функция. Почему не реализована реализация класса Cow?