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 that, I thought of creating a function called menu, and it would be virtual and redefine itself as it was executed. But I’m not succeeding, the code compiled but gives the error Segmentation fault.
It has to do something similar so that it works in principle in this sense, the same as what I’m trying to do on the menu without being what this in the first example?
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();
}
Exact. I fully agree with @bigown. The execution error is really simple: you did not initialize the variable
m
in functionmain
. But you have some mistakes really important of conceptualization there. If you allow me to collaborate, the main reason why it doesn’t make sense to have the menu in the animal class is: this is not an animal’s "behavior". In OO you organize in classes the behaviors that make sense in each entity. It would be more correct to create another class (UI, "User Interface", for example) that would make the menu and create the correct animal. Good luck with your studies. :)– Luiz Vieira
Ah, another thing: be careful not to occur in hammer-and-nail syndrome (the saying says: "for those who only know how to use a hammer, every problem is nail"). Polymorphism is useful, but it’s not the answer to everything. For example, in this other UI class that I suggested, you wouldn’t necessarily need to use polymorphism, okay? :)
– Luiz Vieira
yes I know but more is for a matter of logic even to know if it is possible or not I have other examples here more correct I am doing this to understand if it is possible and even if it works understand nalogica what really aocntece for it to work....
– dark777
@dark777 is what I said, this is not teaching you anything, the polymorphism that makes sense and is obviously possible, is what was in the original example. Luiz Vieira already gave some more tips, I didn’t even say anything, because the example (that wasn’t even on the site) was so out of touch that I thought it would be too advanced.
– Maniero