In the program hereafter, I have a class animal, that has derived classes cat and dog with the same public functions but different private functions. I would like to let the user decide during runtime which animal is being created. I have made a simple example that shows what I approximately want, but which obviously doesn't work. I don't know how to solve this and would like to have your help.
#include <cstdio>
class canimal
{
  public:
    int sound()
    {
      std::printf("...\n");
      return 0;
    }
};
class cdog : public canimal
{
  public:
    int sound()
    {
      std::printf("Woof!\n");
      return 0;
    }
};
class ccat : public canimal
{
  public:
    int sound()
    {
      std::printf("Mieau!\n");
      return 0;
    }
};
int main()
{
  canimal *animal;
  cdog    *dog;
  // I would like to let the user decide here which animal will be made
  // In this case, I would like the function to say "Woof!", but of course it doesn't...
  animal = new cdog;
  animal->sound();
  // Here it works, but I would like the pointer to be of the generic class
  // such that the type of animal can be chosen at runtime
  dog    = new cdog;
  dog->sound();
  return 0;
}
 
     
     
     
    