Menu with Polymorphism

1

I'm creating some examples to learn polymorphism more deeply, but in the original code, all functions work correctly.

Original code:

#include <cstdlib>
#include <iostream>

class Mamifero
{
 protected:
   int idade;

 public:
   Mamifero(){}
   ~Mamifero(){}

   virtual void somMamifero() const
   {
     std::cout<<"\n\tSom de mamifero.\n";
  }
};

class Boi: public Mamifero
{
 public:
   void somMamifero() const
   {
    std::cout<<"\n\tMuu ..! Muu..!!\n";
   }
};

class Gato: public Mamifero
{
 public:
   void somMamifero() const
   {
    std::cout<<"\n\tMiAu ..! MiAu..!!\n";
   }
};

class Porco: public Mamifero
{
 public:
   void somMamifero() const
   {
    std::cout<<"\n\tOinc ..! Oinc..!!\n";
   }
};

class Cachorro: public Mamifero
{
 public:
   void somMamifero() const
   {
    std::cout<<"\n\tAu ..! Au..!!\n";
   }
};

int main()
{
  Mamifero* mamPtr;
  int op;
  while(op != 5)
  {
    std::cout<<"\n\t(1) Boi"
             <<"\n\t(2) Gato"
             <<"\n\t(3) Porco"
             <<"\n\t(4) Cachorro"
             <<"\n\t(5) Sair"
         <<"\n\tDigite: ";
     std::cin>>op;
   switch(op)
    {
     case 1:{
       mamPtr = new Boi();
       mamPtr->somMamifero();
       break;
      }
     case 2:{
       mamPtr = new Gato();
       mamPtr->somMamifero();
       break;
      }
     case 3:{
       mamPtr = new Porco();
       mamPtr->somMamifero();
       break;
      }
     case 4:{
       mamPtr = new Cachorro();
       mamPtr->somMamifero();
       break;
      }
     case 5:{
       std::cout<<"\n\tGood Bye\n\n";
       exit(0);
       break;
      }
     default:
       std::cout<<"\n\tOpção Inválida ..!!!\n";
    }
  }
}

Because of this, I thought about creating a function called menu , and it would be virtual and would be redefined as it was run. But I'm not getting it, the code has compiled but the segmentation fault error.

How can I do something like this to work on this principle, just like what I'm trying to do in the menu without being the first one?

Modified with polymorphic menu giving error:

#include <cstdlib>
#include <iostream>

class Mamifero
{
 public:
   Mamifero(){}
   ~Mamifero(){}

   virtual void somMamifero() const
   {
     std::cout<<"\n\tSom de mamifero.\n";
  }

  virtual void menu() const
  {
   Mamifero* mamPtr;
  int op;
  while(op != 5)
  {
    std::cout<<"\n\t(1) Boi"
             <<"\n\t(2) Gato"
             <<"\n\t(3) Porco"
             <<"\n\t(4) Cachorro"
             <<"\n\t(5) Sair"
         <<"\n\tDigite: ";
     std::cin>>op;
   switch(op)
    {
     case 1:{
       mamPtr = new Mamifero();
       mamPtr->somMamifero();
       break;
      }
     case 2:{
       mamPtr = new Mamifero();
       mamPtr->somMamifero();
       break;
      }
     case 3:{
       mamPtr = new Mamifero();
       mamPtr->somMamifero();
       break;
      }
     case 4:{
       mamPtr = new Mamifero();
       mamPtr->somMamifero();
       break;
      }
     case 5:{
       std::cout<<"\n\tGood Bye\n\n";
       exit(0);
       break;
      }
     default:
       std::cout<<"\n\tOpção Inválida ..!!!\n";
    }
  }
  }

};

class Boi: public Mamifero
{
 public:
   void somMamifero() const
   {
    std::cout<<"\n\tMuu ..! Muu..!!\n";
   }

   void menu() const
   {
    Mamifero* mamPtr;
    mamPtr = new Boi();
    mamPtr->somMamifero();
   }
};

class Gato: public Mamifero
{
 public:
   void somMamifero() const
   {
    std::cout<<"\n\tMiAu ..! MiAu..!!\n";
   }

   void menu() const
   {
    Mamifero* mamPtr;
    mamPtr = new Gato();
    mamPtr->somMamifero();
   }
};

class Porco: public Mamifero
{
 public:
   void somMamifero() const
   {
    std::cout<<"\n\tOinc ..! Oinc..!!\n";
   }

   void menu() const
   {
    Mamifero* mamPtr;
    mamPtr = new Porco();
    mamPtr->somMamifero();
  }
};

class Cachorro: public Mamifero
{
 public:
   void somMamifero() const
   {
    std::cout<<"\n\tAu ..! Au..!!\n";
   }

   void menu() const
   {
    Mamifero* mamPtr;
    mamPtr = new Cachorro();
    mamPtr->somMamifero();
  }
};

int main()
{
  Mamifero *m;
  m->menu();
}
    
asked by anonymous 10.12.2016 / 12:57

1 answer

2

It's quite simple, do not do it. It makes no sense to put Menu() inside Mamifero . It makes even less sense to the method, not function, to be virtual and to use polymorphism. The example looks great as originally intended.

I suggest you learn one concept at a time, and once you master one you must move on to another. I'm sorry, but the way you're trying, you're "unlearning" and will not evolve. Learning must be a ladder where you can not jump up stairs. I know everyone wants to see it working, meaning they want to see the practice. But without mastering theory, it can not program law, especially in object orientation which is an essentially theoretical concept. You have to learn to do the right thing, not to see it working. What works but is wrong is of no use. You need to understand structure and good fonts.

If you still want to see "working", just initialize the variable in Main() so that it can have an instance and call the Menu() method. But this is still very wrong. I will not correct or list all the problems, including in the original, but it would be this:

Mamifero *m = new Mamifero();

See running on ideone .

    
10.12.2016 / 13:32