3
I came from Java and I have a question about polymorphism in C++.
Given these classes:
class Empresa{
vector<Pessoa> pessoas;
int empregados;
addPessoa(Pessoa* p)
}
class Pessoa {
...
};
class Empregado:public Pessoa{
...
}
class Dono:public Pessoa{
...
}
e a função:
addPessoa(Pessoa* p){
pessoas.push_back(*p);
if(...) // pessoa é um empregado
empregados++;
}
I’m not getting to implement this if
.
I don’t know much about C++, but I do know that most solutions in this regard are based on the use of
dynamic_cast
. In other words, you would try to transformp
in a new class objectEmpregado
and test if the pointer is not null.– Tasso Evangelista
What is an employee? Is that a concept represented in the code? I’m just seeing "employee". It’s the same thing?
– Pablo Almeida
Polymorphism is not using
if
. The idea of polymorphism is that you simply make the call of a function and the correct implementation be called based on the type.– Pablo Almeida