Cześć, nie rozumiem czemu wywołuje mi się metoda klasy bazowej, a nie pod klasy. Mam klasę Animal która rozszerza mi klasę Cat. W klasie Cat przesłaniam metodę getVoice. Mam również klasę Herd, w której trzymam swoje zwierzęta. Jest tam także metoda getVoice, która powinna wywoływać odpowiednie metody podklas. Przeciążyłem również operator dodawania, żebym mógł łatwo dodawać zwierzęta do stada. Tak wygląda całość:
class Cat : public Animal
{
public:
Cat();
virtual std::string getVoice();
~Cat();
};
Cat::Cat()
{
}
std::string Cat::getVoice()
{
return "miau";
}
Cat::~Cat()
{
}
class Animal
{
public:
Animal();
virtual std::string getVoice();
~Animal();
};
Animal::Animal()
{
}
std::string Animal::getVoice()
{
return "Animal";
}
Animal::~Animal()
{
}
class Herd
{
public:
Herd();
std::string getVoice();
~Herd();
Herd operator+(Animal& animal);
private:
std::vector <Animal> herd;
};
Herd::Herd()
{
}
std::string Herd::getVoice()
{
std::string voices = "";
for (int i = 0; i < herd.size(); i++)
{
voices += herd[i].getVoice();
}
return voices;
}
Herd::~Herd()
{
}
Herd Herd::operator+(Animal& animal)
{
herd.push_back(animal);
return *this;
}
int main()
{
Herd *herd = new Herd();
Cat * cat = new Cat();
*herd + *cat;
cout << herd->getVoice();
// cout << cat->getVoice();
cout << "\n\n";
}
Gdy uruchomię prinuje mi Animal :/