#include <iostream>
// Animals ---------------------------------------
// Herbivores
class Herbivore abstract
{
protected:
int weight;
bool life;
public:
virtual std::string animal() const = 0;
virtual void eat_grass() = 0;
virtual int getWeight() const = 0;
virtual void printAnimal() const = 0;
};
class Wildebeest : public Herbivore
{
public:
Wildebeest() : Herbivore()
{
this->weight = 150;
this->life = true;
}
std::string animal() const override
{
return "Wildebeest";
}
void eat_grass() override
{
weight += 10;
}
int getWeight() const override
{
return weight;
}
void printAnimal() const override
{
std::cout << std::endl;
std::cout << animal() << std::endl;
std::cout << "\t weight - " << weight << std::endl;
std::cout << "\t life - " << life << std::endl;
}
};
class Bison : public Herbivore
{
public:
Bison() : Herbivore()
{
this->weight = 400;
this->life = true;
}
std::string animal() const override
{
return "Bison";
}
void eat_grass() override
{
weight += 10;
}
int getWeight() const override
{
return weight;
}
void printAnimal() const override
{
std::cout << std::endl;
std::cout << animal() << std::endl;
std::cout << "\t weight - " << weight << std::endl;
std::cout << "\t life - " << life << std::endl;
}
};
// Predators
class Predator abstract
{
protected:
int power;
public:
virtual std::string animal() const = 0;
virtual void eat(Herbivore animal) = 0;
virtual void printAnimal() const = 0;
};
class Lion : public Predator
{
public:
Lion() : Predator()
{
this->power = 200;
}
std::string animal() const override
{
return "Lion";
}
void eat(Herbivore animal) override
{
power > animal.getWeight() ? power += 10 : power -= 10;
}
void printAnimal() const override
{
std::cout << std::endl;
std::cout << animal() << std::endl;
std::cout << "\t power - " << power << std::endl;
}
};
class Wolf : public Predator
{
public:
Wolf() : Predator()
{
this->power = 170;
}
std::string animal() const override
{
return "Wolf";
}
void eat(Herbivore animal) override
{
power > animal.getWeight() ? power += 10 : power -= 10;
}
void printAnimal() const override
{
std::cout << std::endl;
std::cout << animal() << std::endl;
std::cout << "\t power - " << power << std::endl;
}
};
// Continent -------------------------------------
class Continent abstract
{
public:
virtual Herbivore* createWildebeest() const = 0;
virtual Predator* createPredator() const = 0;
};
class Africa : public Continent
{
public:
Herbivore* createWildebeest() const override
{
return new Wildebeest();
}
Predator* createPredator() const override
{
return new Lion();
}
};
class NorthAmerica : public Continent
{
public:
Herbivore* createWildebeest() const override
{
return new Bison();
}
Predator* createPredator() const override
{
return new Wolf();
}
};
// Animals World----------------------------------
void AnimalsWorld(const Continent& continent)
{
const Lion* lion = continent.createPredator();
}
Ошибку выдает в строке const Lion* lion = continent.createPredator();
выглядит она так - значение типа "Predator *" нельзя использовать для инициализации сущности типа "const Lion *"