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();
}